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