Fawkes API  Fawkes Development Version
colormap.cpp
1 
2 /**************************************************************************
3  * colormap.h - colormap interface
4  *
5  * Created: Sat Mar 29 12:45:29 2008
6  * Copyright 2005-2008 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 <fvutils/colormap/colormap.h>
25 
26 #include <fvutils/color/color_object_map.h>
27 #include <cstring>
28 
29 namespace firevision {
30 #if 0 /* just to make Emacs auto-indent happy */
31 }
32 #endif
33 
34 /** @class Colormap <fvutils/colormap/colormap.h>
35  * Colormap interface.
36  * This C++ pure virtual class describes the interface to a generic colormap. It is
37  * currently tailored to the YUV colorspace.
38  *
39  * @author Tim Niemueller
40  *
41  *
42  * @fn color_t Colormap::determine(unsigned int y, unsigned int u, unsigned int v) const = 0
43  * Determine color class for given YUV value.
44  * @param y Y value from YUV colorspace
45  * @param u U value from YUV colorspace
46  * @param v V value from YUV colorspace
47  * @return color class for the given YUV color
48  *
49  * @fn void Colormap::set(unsigned int y, unsigned int u, unsigned int v, color_t c) = 0
50  * Set color class for given YUV value.
51  * @param y Y value from YUV colorspace
52  * @param u U value from YUV colorspace
53  * @param v V value from YUV colorspace
54  * @param c class for the given YUV color
55  *
56  * @fn void Colormap::reset() = 0
57  * Reset colormap.
58  * Resets all values to return C_UNKNOWN for every query with determine().
59  *
60  * @fn void Colormap::set(unsigned char *buffer) = 0
61  * Set to the given raw buffer.
62  * @param buffer buffer to copy data from
63  *
64  * @fn size_t Colormap::size() = 0
65  * Size in bytes of buffer returned by get_buffer().
66  * @return size in bytes of buffer returned by get_buffer()
67  *
68  * @fn unsigned char * Colormap::get_buffer() const = 0
69  * Get the raw buffer of this colormap.
70  * @return raw buffer
71  *
72  * @fn Colormap & Colormap::operator+=(const Colormap & cmlt) = 0
73  * Adds the given colormap to this colormap.
74  * This operator takes the given colormap and compares it to this colormap. If this colormap
75  * has C_OTHER or C_BACKGROUND the value is compied from the other LUT, otherwise the
76  * value is kept as is.
77  * @param cmlt other colormap to add
78  * @return reference to this
79  *
80  * @fn Colormap & Colormap::operator+=(const char *filename) = 0
81  * Convenience method for the method above.
82  * This adds the colormap as in the above method but instead of an instantiated colormap
83  * it takes the path to a colormap file which is loaded and added.
84  * @param filename file name of colormap to add
85  * @return reference to this
86  *
87  * @fn unsigned int Colormap::width() const = 0
88  * Get width of colormap.
89  * @return colormap width, meaning depends on actual colormap implementation
90  *
91  * @fn unsigned int Colormap::height() const = 0
92  * Get height of colormap.
93  * @return colormap height, meaning depends on actual colormap implementation
94  *
95  * @fn unsigned int Colormap::depth() const = 0
96  * Get depth of colormap.
97  * @return colormap depth, meaning depends on actual colormap implementation
98  *
99  * @fn unsigned int Colormap::deepness() const = 0
100  * Get deepness of colormap.
101  * The deepness is the maximum value of depth().
102  * @return colormap deepness, meaning depends on actual colormap implementation
103  *
104  * @fn std::list<ColormapFileBlock *> Colormap::get_blocks() = 0
105  * Get file blocks for this colormap.
106  * @return list of colormap blocks for this colormap.
107  *
108  */
109 
110 /** Virtual empty destructor. */
112 {
113 }
114 
115 /** Create image from LUT.
116  * Create image from LUT, useful for debugging and analysing.
117  * This method produces a representation of the given level
118  * (Y range with 0 <= level < depth) for visual inspection of the colormap.
119  * The dimensions of the resulting image are 512x512 pixels. It uses standard strong
120  * colors for the different standard color classes. C_UNKNOWN is grey, C_BACKGROUND
121  * is black (like C_BLACK).
122  * If the standard method does not suit your needs you can override this method.
123  * @param yuv422_planar_buffer contains the image upon return, must be initialized
124  * with the appropriate memory size before calling, dimensions are 512x512 pixels.
125  * @param level the level to draw, it's a range in the Y direction and is in the
126  * range 0 <= level < depth.
127  */
128 void
129 Colormap::to_image(unsigned char *yuv422_planar_buffer, unsigned int level)
130 {
131  unsigned int iwidth = image_width() / 2;
132  unsigned int iheight = image_height() / 2;
133 
134  unsigned int lwidth = width();
135  unsigned int lheight = height();
136 
137  unsigned int pixel_per_step = iheight / lheight;
138  unsigned int lines_per_step = iwidth / lwidth;
139 
140  unsigned char *yp = yuv422_planar_buffer;
141  unsigned char *up = YUV422_PLANAR_U_PLANE(yuv422_planar_buffer, iwidth * 2, iheight * 2);
142  unsigned char *vp = YUV422_PLANAR_V_PLANE(yuv422_planar_buffer, iwidth * 2, iheight * 2);
143 
144  unsigned int y = level * deepness() / depth();
145 
146  YUV_t c;
147  for (unsigned int v = lwidth; v > 0 ; --v) {
148  unsigned int v_index = (v - 1) * deepness() / lwidth;
149  for (unsigned int u = 0; u < lheight; ++u) {
150  unsigned int u_index = u * deepness() / lheight;
151  c = ColorObjectMap::get_color(determine(y, u_index, v_index));
152 
153  for (unsigned int p = 0; p < pixel_per_step; ++p) {
154  *yp++ = c.Y;
155  *yp++ = c.Y;
156  *up++ = c.U;
157  *vp++ = c.V;
158  }
159  }
160  // Double line
161  unsigned int lines = (2 * (lines_per_step - 1)) + 1;
162  memcpy(yp, (yp - iwidth * 2), (iwidth * 2) * lines);
163  yp += (iwidth * 2) * lines;
164  memcpy(up, (up - iwidth), iwidth * lines);
165  memcpy(vp, (vp - iwidth), iwidth * lines);
166  up += iwidth * lines;
167  vp += iwidth * lines;
168  }
169 }
170 
171 /** Width of conversion image.
172  * The buffer passed into to_image() must have the returned width.
173  * @return required width for colormap visualization image
174  */
175 unsigned int
177 {
178  return 512;
179 }
180 
181 /** Height of conversion image.
182  * The buffer passed into to_image() must have the returned width.
183  * @return required width for colormap visualization image
184  */
185 unsigned int
187 {
188  return 512;
189 }
190 
191 
192 } // end namespace firevision
unsigned char V
V component.
Definition: yuv.h:62
virtual void to_image(unsigned char *yuv422_planar_buffer, unsigned int level=0)
Create image from LUT.
Definition: colormap.cpp:129
virtual unsigned int depth() const =0
Get depth of colormap.
virtual unsigned int height() const =0
Get height of colormap.
unsigned char Y
Y component.
Definition: yuv.h:60
virtual ~Colormap()
Virtual empty destructor.
Definition: colormap.cpp:111
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const =0
Determine color class for given YUV value.
virtual unsigned int width() const =0
Get width of colormap.
unsigned char U
U component.
Definition: yuv.h:61
virtual unsigned int deepness() const =0
Get deepness of colormap.
YUV pixel.
Definition: yuv.h:59
virtual unsigned int image_width() const
Width of conversion image.
Definition: colormap.cpp:176
static YUV_t get_color(color_t color)
YUV_t getter.
virtual unsigned int image_height() const
Height of conversion image.
Definition: colormap.cpp:186