segment_color.cpp

00001
00002 /***************************************************************************
00003  *  segment_color.cpp - Implementation of color segmentation filter
00004  *                      This filter can be used to draw the segmentation for
00005  *                      all objects into a colored YUV422_PLANAR buffer
00006  *
00007  *  Created: Mon Jul 04 16:18:15 2005
00008  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00009  *
00010  ****************************************************************************/
00011
00012 /*  This program is free software; you can redistribute it and/or modify
00013  *  it under the terms of the GNU General Public License as published by
00014  *  the Free Software Foundation; either version 2 of the License, or
00015  *  (at your option) any later version. A runtime exception applies to
00016  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00017  *
00018  *  This program is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU Library General Public License for more details.
00022  *
00023  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00024  */
00025
00026 #include <filters/segment_color.h>
00027
00028 #include <models/color/colormodel.h>
00029 #include <fvutils/color/yuv.h>
00030 #include <cstddef>
00031 
00032 /** @class FilterColorSegmentation <filters/segment_color.h>
00033  * Segmentation filter.
00034  * Visually marks pixels depending of their classification determined by the
00035  * supplied color model to make the segmentation visible.
00036  * The pixels are marked with the color matching the segmentation with an
00037  * appropriate place holder color
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor.
00042  * @param cm color model to use
00043  */
00044 FilterColorSegmentation::FilterColorSegmentation(ColorModel *cm)
00045   : Filter("FilterColorSegmentation")
00046 {
00047   this->cm = cm;
00048 }
00049
00050
00051 void
00052 FilterColorSegmentation::apply()
00053 {
00054   register unsigned int h = 0;
00055   register unsigned int w = 0;
00056
00057   // source y-plane
00058   register unsigned char *yp   = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step);
00059   // source u-plane
00060   register unsigned char *up   = YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00061                                    + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2) ;
00062   // source v-plane
00063   register unsigned char *vp   = YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00064                                    + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
00065
00066   // destination y-plane
00067   register unsigned char *dyp  = dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step);
00068   // destination u-plane
00069   register unsigned char *dup  = YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00070                                    + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2) ;
00071   // destination v-plane
00072   register unsigned char *dvp  = YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
00073                                    + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
00074
00075   // line starts
00076   unsigned char *lyp  = yp;   // source y-plane
00077   unsigned char *lup  = up;   // source u-plane
00078   unsigned char *lvp  = vp;   // source v-plane
00079   unsigned char *ldyp = dyp;  // destination y-plane
00080   unsigned char *ldup = dup;  // destination y-plane
00081   unsigned char *ldvp = dvp;  // destination y-plane
00082
00083   color_t c1;
00084   // Unused for now: color_t c2;
00085
00086   for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
00087     for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
00088       c1 = cm->determine(*yp++, *up++, *vp++);
00089       *yp++;
00090        //c2 = cm->determine(*yp++, *up++, *vp++);
00091
00092       switch (c1) {
00093       case C_ORANGE:
00094         *dyp++ = 128;
00095         *dyp++ = 128;
00096         *dup++ = 0;
00097         *dvp++ = 255;
00098         break;
00099       case C_MAGENTA:
00100         *dyp++ = 128;
00101         *dyp++ = 128;
00102         *dup++ = 128;
00103         *dvp++ = 255;
00104         break;
00105       case C_CYAN:
00106         *dyp++ = 128;
00107         *dyp++ = 128;
00108         *dup++ = 255;
00109         *dvp++ = 0;
00110         break;
00111       case C_BLUE:
00112         *dyp++ = 128;
00113         *dyp++ = 128;
00114         *dup++ = 255;
00115         *dvp++ = 128;
00116         break;
00117       case C_YELLOW:
00118         *dyp++ = 255;
00119         *dyp++ = 255;
00120         *dup++ = 0;
00121         *dvp++ = 128;
00122         break;
00123       case C_GREEN:
00124         *dyp++ = 128;
00125         *dyp++ = 128;
00126         *dup++ = 0;
00127         *dvp++ = 0;
00128         break;
00129       case C_WHITE:
00130         *dyp++ = 255;
00131         *dyp++ = 255;
00132         *dup++ = 128;
00133         *dvp++ = 128;
00134         break;
00135       case C_RED:
00136         *dyp++ = 196;
00137         *dyp++ = 196;
00138         *dup++ = 0;
00139         *dvp++ = 255;
00140         break;
00141       default:
00142         *dyp++ = 0;
00143         *dyp++ = 0;
00144         *dup++ = 128;
00145         *dvp++ = 128;
00146         break;
00147       }
00148     }
00149     lyp  += src_roi[0]->line_step;
00150     lup  += src_roi[0]->line_step / 2;
00151     lvp  += src_roi[0]->line_step / 2;
00152     ldyp += dst_roi->line_step;
00153     ldup += dst_roi->line_step / 2;
00154     ldvp += dst_roi->line_step / 2;
00155     yp    = lyp;
00156     up    = lup;
00157     vp    = lvp;
00158     dyp   = ldyp;
00159     dup   = ldup;
00160     dvp   = ldvp;
00161   }
00162
00163 }