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