dilation.cpp

00001 
00002 /***************************************************************************
00003  *  dilation.cpp - implementation of morphological dilation filter
00004  *
00005  *  Created: Thu May 25 15:47:01 2006
00006  *  Copyright  2005-2007  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 <filters/morphology/dilation.h>
00025 
00026 #include <fvutils/color/yuv.h>
00027 #include <core/exception.h>
00028 
00029 #include <cstddef>
00030 #include <ippi.h>
00031 
00032 namespace firevision {
00033 #if 0 /* just to make Emacs auto-indent happy */
00034 }
00035 #endif
00036 
00037 /** @class FilterDilation <filters/morphology/dilation.h>
00038  * Morphological dilation.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor. */
00044 FilterDilation::FilterDilation()
00045   : MorphologicalFilter("Morphological Dilation")
00046 {
00047 }
00048 
00049 
00050 /** Constructor with parameters.
00051  * @param se structuring element buffer. This is just a line-wise concatenated array
00052  * of values. A value of zero means ignore, any other value means to consider this
00053  * value.
00054  * @param se_width width of structuring element
00055  * @param se_height height of structuring element
00056  * @param se_anchor_x x coordinate of anchor in structuring element
00057  * @param se_anchor_y y coordinate of anchor in structuring element
00058  */
00059 FilterDilation::FilterDilation(unsigned char *se,
00060                                unsigned int se_width, unsigned int se_height,
00061                                unsigned int se_anchor_x, unsigned int se_anchor_y)
00062   : MorphologicalFilter("Morphological Dilation")
00063 {
00064   this->se        = se;
00065   this->se_width  = se_width;
00066   this->se_height = se_height;
00067   this->se_anchor_x = se_anchor_x;
00068   this->se_anchor_y = se_anchor_y;
00069 }
00070 
00071 
00072 void
00073 FilterDilation::apply()
00074 {
00075   IppStatus status;
00076 
00077   if ( se == NULL ) {
00078     // standard 3x3 dilation
00079 
00080     IppiSize size;
00081     size.width = src_roi[0]->width - 2;
00082     size.height = src_roi[0]->height - 2;
00083 
00084 
00085     if ( (dst == NULL) || (dst == src[0]) ) {
00086       // In-place
00087 
00088       // std::cout << "Running in-place with standard SE" << std::endl;
00089 
00090       status = ippiDilate3x3_8u_C1IR(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step) + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
00091                                      src_roi[0]->line_step,
00092                                      size);
00093       
00094     } else {
00095       // std::cout << "Running not in-place dilation with standard SE" << std::endl;
00096 
00097       status = ippiDilate3x3_8u_C1R(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step) + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
00098                                     src_roi[0]->line_step,
00099                                     dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
00100                                     dst_roi->line_step,
00101                                     size);
00102 
00103       yuv422planar_copy_uv(src[0], dst,
00104                            src_roi[0]->image_width, src_roi[0]->image_height,
00105                            src_roi[0]->start.x, src_roi[0]->start.y,
00106                            src_roi[0]->width, src_roi[0]->height );
00107     }
00108   } else {
00109     // we have a custom SE
00110 
00111     IppiSize size;
00112     size.width = src_roi[0]->width - se_width;
00113     size.height = src_roi[0]->height - se_width;
00114 
00115     IppiSize mask_size = { se_width, se_height };
00116     IppiPoint mask_anchor = { se_anchor_x, se_anchor_y };
00117 
00118     /*
00119     std::cout << "Dilation filter is running with the following parameters:" << std::endl
00120               << "  ROI size:    " << size.width << " x " << size.height << std::endl
00121               << "  mask size:   " << mask_size.width << " x " << mask_size.height << std::endl
00122               << "  mask anchor: (" << mask_anchor.x  << "," << mask_anchor.y << ")" << std::endl
00123               << std::endl;
00124 
00125     printf("  src buf:     0x%x\n", (unsigned int)src );
00126     printf("  dst buf:     0x%x\n", (unsigned int)dst );
00127     */
00128 
00129     if ( (dst == NULL) || (dst == src[0]) ) {
00130       // In-place
00131 
00132       status = ippiDilate_8u_C1IR(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step) + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
00133                                   src_roi[0]->line_step,
00134                                   size,
00135                                   se, mask_size, mask_anchor);
00136       
00137     } else {
00138       //std::cout << "Running NOT in-place" << std::endl;
00139 
00140       status = ippiDilate_8u_C1R(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step) + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
00141                                  src_roi[0]->line_step,
00142                                  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
00143                                  dst_roi->line_step,
00144                                  size,
00145                                  se, mask_size, mask_anchor);
00146 
00147       yuv422planar_copy_uv(src[0], dst,
00148                            src_roi[0]->image_width, src_roi[0]->image_height,
00149                            src_roi[0]->start.x, src_roi[0]->start.y,
00150                            src_roi[0]->width, src_roi[0]->height );
00151 
00152     }
00153   }
00154 
00155   if ( status != ippStsNoErr ) {
00156     throw fawkes::Exception("Morphological dilation failed with %i\n", status);
00157   }
00158 
00159 }
00160 
00161 } // end namespace firevision

Generated on Tue Feb 22 13:32:15 2011 for Fawkes API by  doxygen 1.4.7