border_shrinker.cpp

00001
00002 /***************************************************************************
00003  *  border_shrinker.cpp - Implementation of BorderShrinker
00004  *
00005  *  Generated: Wed Feb 15 2005 15:02:56
00006  *  Copyright  2005-2006  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 <classifiers/border_shrinker.h>
00025
00026 #include <fvutils/color/colorspaces.h>
00027 #include <fvutils/base/roi.h>
00028
00029 #include <models/scanlines/scanlinemodel.h>
00030 #include <models/color/colormodel.h>
00031
00032 #include <cstddef>
00033 
00034 /** @class BorderShrinker <classifiers/border_shrinker.h>
00035  * Border shrinker.
00036  * This shrinker makes sure that a ROI does not get too close to the image
00037  * boundaries. This may be needed for some mask-based operations.
00038  *
00039  */
00040 
00041 /** Constructor.
00042  * @param border_left minimum x value for ROI
00043  * @param border_right maximum x plus width value for ROI
00044  * @param border_top minimum y value for ROI
00045  * @param border_bottom maximum y plus height value for ROI
00046  */
00047 BorderShrinker::BorderShrinker(unsigned int border_left, unsigned int border_right,
00048                              unsigned int border_top, unsigned int border_bottom)
00049   : Shrinker()
00050 {
00051   src = NULL;
00052   this->border_left   = border_left;
00053   this->border_right  = border_right;
00054   this->border_top    = border_top;
00055   this->border_bottom = border_bottom;
00056 }
00057
00058 
00059 /** Virtual empty destructor. */
00060 BorderShrinker::~BorderShrinker()
00061 {
00062 }
00063
00064 
00065 /** Shrink!
00066  * Do the actual shrinking.
00067  * @param roi ROI to shrink
00068  */
00069 void
00070 BorderShrinker::shrink( ROI *roi )
00071 {
00072   unsigned int brdr; // border
00073
00074   // bottom
00075   if (border_bottom > 0) {
00076     brdr = roi->image_height - border_bottom;
00077     if (roi->start.y >= brdr) {
00078       roi->height = 0;
00079     } else if ((roi->start.y + roi->height) > brdr) {
00080       roi->height -= (roi->start.y + roi->height) - brdr;
00081     }
00082   }
00083
00084   // top
00085   if (border_top > 0) {
00086     brdr = border_top;
00087     if (roi->start.y <= brdr) {
00088       roi->height = 0;
00089     } else if ((roi->start.y + roi->height) < brdr) {
00090       roi->start.y = brdr;
00091       roi->height -= (roi->start.y + roi->height) - brdr;
00092     }
00093   }
00094
00095   // right
00096   if (border_right > 0) {
00097     brdr = roi->image_width - border_right;
00098     if (roi->start.x >= brdr) {
00099       roi->width = 0;
00100     } else if ((roi->start.x + roi->width) > brdr) {
00101       roi->width -= (roi->start.x + roi->width) - brdr;
00102     }
00103   }
00104
00105   // left
00106   if (border_left > 0) {
00107     brdr = border_left;
00108     if (roi->start.x <= brdr) {
00109       roi->width = 0;
00110     } else if ((roi->start.x + roi->width) < brdr) {
00111       roi->start.x = brdr;
00112       roi->width -= (roi->start.x + roi->width) - brdr;
00113     }
00114   }
00115
00116 }