threshold.cpp

00001
00002 /***************************************************************************
00003  *  threshold.cpp - Implementation for threshold filter, this filter will
00004  *                  luminance values below a given threshold to the given
00005  *                  min_replace value, values above a given max threshold
00006  *                  will be set to the max_replace value
00007  *
00008  *  Created: Tue Jun 07 14:30:10 2005
00009  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00010  *
00011  ****************************************************************************/
00012
00013 /*  This program is free software; you can redistribute it and/or modify
00014  *  it under the terms of the GNU General Public License as published by
00015  *  the Free Software Foundation; either version 2 of the License, or
00016  *  (at your option) any later version. A runtime exception applies to
00017  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00018  *
00019  *  This program is distributed in the hope that it will be useful,
00020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *  GNU Library General Public License for more details.
00023  *
00024  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00025  */
00026
00027 #include <filters/threshold.h>
00028
00029 #include <core/exception.h>
00030
00031 #include <cstddef>
00032 #include <ippi.h>
00033 
00034 /** @class FilterThreshold <filters/threshold.h>
00035  * Threshold filter
00036  */
00037 
00038 /** Constructor.
00039  * @param min minimum value
00040  * @param min_replace values below min are replaced with this value
00041  * @param max maximum value
00042  * @param max_replace values above max are replaced with this value
00043  */
00044 FilterThreshold::FilterThreshold(unsigned char min, unsigned char min_replace,
00045                                  unsigned char max, unsigned char max_replace)
00046   : Filter("FilterThreshold")
00047 {
00048   this->min = min;
00049   this->max = max;
00050   this->min_replace = min_replace;
00051   this->max_replace = max_replace;
00052 }
00053
00054 
00055 /** Set new thresholds.
00056  * @param min minimum value
00057  * @param min_replace values below min are replaced with this value
00058  * @param max maximum value
00059  * @param max_replace values above max are replaced with this value
00060  */
00061 void
00062 FilterThreshold::set_thresholds(unsigned char min, unsigned char min_replace,
00063                                 unsigned char max, unsigned char max_replace)
00064 {
00065   this->min = min;
00066   this->max = max;
00067   this->min_replace = min_replace;
00068   this->max_replace = max_replace;
00069 }
00070
00071
00072 void
00073 FilterThreshold::apply()
00074 {
00075   IppiSize size;
00076   size.width = src_roi[0]->width;
00077   size.height = src_roi[0]->height;
00078
00079   IppStatus status;
00080
00081   if ((dst == NULL) || (dst == src[0])) {
00082     // In-place
00083     status = ippiThreshold_GTVal_8u_C1IR( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
00084                                           size, max, max_replace );
00085     if ( status == ippStsNoErr ) {
00086       status = ippiThreshold_LTVal_8u_C1IR( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
00087                                             size, min, min_replace );
00088     }
00089   } else {
00090     //                                base + number of bytes to line y              + pixel bytes
00091     status = ippiThreshold_GTVal_8u_C1R( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
00092                                          dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step), dst_roi->line_step,
00093                                          size, max, max_replace );
00094
00095     if ( status == ippStsNoErr ) {
00096       status = ippiThreshold_LTVal_8u_C1R( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
00097                                            dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step), dst_roi->line_step,
00098                                            size, min, min_replace );
00099     }
00100   }
00101
00102   if ( status != ippStsNoErr ) {
00103     throw fawkes::Exception("Threshold filter failed with %i\n", status);
00104   }
00105
00106 }