colormap.cpp

00001 
00002 /**************************************************************************
00003  *  colormap.h - colormap interface
00004  *
00005  *  Created: Sat Mar 29 12:45:29 2008
00006  *  Copyright  2005-2008  Tim Niemueller  [www.niemueller.de]
00007  *
00008  ***************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <fvutils/colormap/colormap.h>
00025 
00026 #include <fvutils/color/color_object_map.h>
00027 #include <cstring>
00028 
00029 namespace firevision {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031 }
00032 #endif
00033 
00034 /** @class Colormap <fvutils/colormap/colormap.h>
00035  * Colormap interface.
00036  * This C++ pure virtual class describes the interface to a generic colormap. It is
00037  * currently tailored to the YUV colorspace.
00038  *
00039  * @author Tim Niemueller
00040  *
00041  *
00042  * @fn color_t Colormap::determine(unsigned int y, unsigned int u, unsigned int v) const = 0
00043  * Determine color class for given YUV value.
00044  * @param y Y value from YUV colorspace
00045  * @param u U value from YUV colorspace
00046  * @param v V value from YUV colorspace
00047  * @return color class for the given YUV color
00048  *
00049  * @fn void Colormap::set(unsigned int y, unsigned int u, unsigned int v, color_t c) = 0
00050  * Set color class for given YUV value.
00051  * @param y Y value from YUV colorspace
00052  * @param u U value from YUV colorspace
00053  * @param v V value from YUV colorspace
00054  * @param c class for the given YUV color
00055  *
00056  * @fn void Colormap::reset() = 0
00057  * Reset colormap.
00058  * Resets all values to return C_UNKNOWN for every query with determine().
00059  *
00060  * @fn void Colormap::set(unsigned char *buffer) = 0
00061  * Set to the given raw buffer.
00062  * @param buffer buffer to copy data from
00063  *
00064  * @fn size_t Colormap::size() = 0
00065  * Size in bytes of buffer returned by get_buffer().
00066  * @return size in bytes of buffer returned by get_buffer()
00067  *
00068  * @fn unsigned char * Colormap::get_buffer() const = 0
00069  * Get the raw buffer of this colormap.
00070  * @return raw buffer
00071  *
00072  * @fn Colormap &  Colormap::operator+=(const Colormap & cmlt) = 0
00073  * Adds the given colormap to this colormap.
00074  * This operator takes the given colormap and compares it to this colormap. If this colormap
00075  * has C_OTHER or C_BACKGROUND the value is compied from the other LUT, otherwise the
00076  * value is kept as is.
00077  * @param cmlt other colormap to add
00078  * @return reference to this
00079  *
00080  * @fn Colormap & Colormap::operator+=(const char *filename) = 0
00081  * Convenience method for the method above.
00082  * This adds the colormap as in the above method but instead of an instantiated colormap
00083  * it takes the path to a colormap file which is loaded and added.
00084  * @param filename file name of colormap to add
00085  * @return reference to this
00086  *
00087  * @fn unsigned int Colormap::width() const = 0
00088  * Get width of colormap.
00089  * @return colormap width, meaning depends on actual colormap implementation
00090  *
00091  * @fn unsigned int Colormap::height() const = 0
00092  * Get height of colormap.
00093  * @return colormap height, meaning depends on actual colormap implementation
00094  *
00095  * @fn unsigned int Colormap::depth() const = 0
00096  * Get depth of colormap.
00097  * @return colormap depth, meaning depends on actual colormap implementation
00098  *
00099  * @fn unsigned int Colormap::deepness() const = 0
00100  * Get deepness of colormap.
00101  * The deepness is the maximum value of depth().
00102  * @return colormap deepness, meaning depends on actual colormap implementation
00103  *
00104  * @fn std::list<ColormapFileBlock *>  Colormap::get_blocks() = 0
00105  * Get file blocks for this colormap.
00106  * @return list of colormap blocks for this colormap.
00107  *
00108  */
00109 
00110 /** Virtual empty destructor. */
00111 Colormap::~Colormap()
00112 {
00113 }
00114 
00115 /** Create image from LUT.
00116  * Create image from LUT, useful for debugging and analysing.
00117  * This method produces a representation of the given level
00118  * (Y range with 0 <= level < depth) for visual inspection of the colormap.
00119  * The dimensions of the resulting image are 512x512 pixels. It uses standard strong
00120  * colors for the different standard color classes. C_UNKNOWN is grey, C_BACKGROUND
00121  * is black (like C_BLACK).
00122  * If the standard method does not suit your needs you can override this method.
00123  * @param yuv422_planar_buffer contains the image upon return, must be initialized
00124  * with the appropriate memory size before calling, dimensions are 512x512 pixels.
00125  * @param level the level to draw, it's a range in the Y direction and is in the
00126  * range 0 <= level < depth.
00127  */
00128 void
00129 Colormap::to_image(unsigned char *yuv422_planar_buffer, unsigned int level)
00130 {
00131   unsigned int iwidth  = image_width()  / 2;
00132   unsigned int iheight = image_height() / 2;
00133 
00134   unsigned int lwidth  = width();
00135   unsigned int lheight = height();
00136 
00137   unsigned int pixel_per_step = iheight / lheight;
00138   unsigned int lines_per_step = iwidth  / lwidth;
00139 
00140   unsigned char *yp = yuv422_planar_buffer;
00141   unsigned char *up = YUV422_PLANAR_U_PLANE(yuv422_planar_buffer, iwidth * 2, iheight * 2);
00142   unsigned char *vp = YUV422_PLANAR_V_PLANE(yuv422_planar_buffer, iwidth * 2, iheight * 2);
00143 
00144   unsigned int y = level * deepness() / depth();
00145 
00146   YUV_t c;
00147   for (unsigned int v = lwidth; v > 0 ; --v) {
00148     unsigned int v_index = (v - 1) * deepness() / lwidth;
00149     for (unsigned int u = 0; u < lheight; ++u) {
00150       unsigned int u_index = u * deepness() / lheight;
00151       c = ColorObjectMap::get_color(determine(y, u_index, v_index));
00152 
00153       for (unsigned int p = 0; p < pixel_per_step; ++p) {
00154         *yp++ = c.Y;
00155         *yp++ = c.Y;
00156         *up++ = c.U;
00157         *vp++ = c.V;
00158       }
00159     }
00160     // Double line
00161     unsigned int lines = (2 * (lines_per_step - 1)) + 1;
00162     memcpy(yp, (yp - iwidth * 2), (iwidth * 2) * lines);
00163     yp += (iwidth * 2) * lines;
00164     memcpy(up, (up - iwidth), iwidth * lines);
00165     memcpy(vp, (vp - iwidth), iwidth * lines);
00166     up += iwidth * lines;
00167     vp += iwidth * lines;
00168   }
00169 }
00170 
00171 /** Width of conversion image.
00172  * The buffer passed into to_image() must have the returned width.
00173  * @return required width for colormap visualization image
00174  */
00175 unsigned int
00176 Colormap::image_width() const
00177 {
00178   return 512;
00179 }
00180 
00181 /** Height of conversion image.
00182  * The buffer passed into to_image() must have the returned width.
00183  * @return required width for colormap visualization image
00184  */
00185 unsigned int
00186 Colormap::image_height() const
00187 {
00188   return 512;
00189 }
00190 
00191 
00192 } // end namespace firevision