Dims.H

Go to the documentation of this file.
00001 /*!@file Image/Dims.H A simple struct to hold a pair of width/height dimensions.
00002  */
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Dims.H $
00035 // $Id: Dims.H 5755 2005-10-19 20:57:27Z rjpeters $
00036 //
00037 
00038 #ifndef DIMS_H_DEFINED
00039 #define DIMS_H_DEFINED
00040 
00041 #include "Util/Assert.H"
00042 #include "Util/Types.H"
00043 
00044 #include <string> // for string conversions
00045 
00046 //! A simple struct to hold a pair of width/height dimensions.
00047 /*! We guarantee that the width and height are always non-negative, though not
00048     necessarily strictly positive. The default Dims are the 0-by-0
00049     dimensions. Dims is strictly a const class; the width/height can only be
00050     set at construction. */
00051 class Dims
00052 {
00053 public:
00054   //! Default construct with width and height == 0.
00055   inline Dims() throw();
00056 
00057   //! Construct from a given width + height, which must be non-negative.
00058   inline Dims(int width, int height) throw();
00059 
00060   //! Copy construct.
00061   inline Dims(const Dims& other) throw();
00062 
00063   // Default copy-ctor and assignment-operator OK.
00064 
00065   //! Returns the width, will always be non-negative.
00066   inline int w() const throw();
00067 
00068   //! Returns the height, will always be non-negative.
00069   inline int h() const throw();
00070 
00071   //! Returns the size of the 2-D area specified by the dimensions.
00072   /*! (i.e. width * height). */
00073   inline int sz() const throw();
00074 
00075   //! Returns the larger of the width and height.
00076   inline int max() const throw();
00077 
00078   //! Returns the smaller of the width and height.
00079   inline int min() const throw();
00080 
00081   //! Query whether the 2-D array is empty
00082   /*! (empty means that at least one of the width or height is 0). */
00083   inline bool isEmpty() const throw();
00084 
00085   //! Query whether the 2-D array is non-empty
00086   /*! (i.e. both width and height are positive, non-zero). */
00087   inline bool isNonEmpty() const throw();
00088 
00089   //! See if two Dims are the same.
00090   inline bool operator==(const Dims& x) const throw();
00091 
00092   //! See if two Dims are different.
00093   inline bool operator!=(const Dims& x) const throw();
00094 
00095 private:
00096   int ww;  //!< The width.
00097   int hh;  //!< The height.
00098 };
00099 
00100 
00101 //! Dims overload: format is "<int>x<int>"
00102 std::string convertToString(const Dims& val);
00103 
00104 //! Dims overload: format is "<int>x<int>"
00105 void convertFromString(const std::string& str, Dims& val);
00106 
00107 
00108 // ######################################################################
00109 // ######################################################################
00110 // INLINE free functions for Dims:
00111 // ######################################################################
00112 // ######################################################################
00113 
00114 /// Dims+Dims addition
00115 inline Dims operator+(const Dims& d1, const Dims& d2)
00116 {
00117   return Dims(d1.w()+d2.w(), d1.h()+d2.h());
00118 }
00119 
00120 /// Dims+int addition
00121 inline Dims operator+(const Dims& d, int a)
00122 {
00123   return Dims(d.w()+a, d.h()+a);
00124 }
00125 
00126 /// int+Dims addition
00127 inline Dims operator+(int a, const Dims& d)
00128 {
00129   return Dims(d.w()+a, d.h()+a);
00130 }
00131 
00132 /// Dims-int subtraction
00133 inline Dims operator-(const Dims& d, int a)
00134 {
00135   return Dims(d.w()-a, d.h()-a);
00136 }
00137 
00138 /// Dims*int multiplication
00139 inline Dims operator*(const Dims& d, int factor)
00140 {
00141   return Dims(d.w()*factor, d.h()*factor);
00142 }
00143 
00144 /// Dims*double multiplication
00145 inline Dims operator*(const Dims& d, double factor)
00146 {
00147   return Dims(int(d.w()*factor), int(d.h()*factor));
00148 }
00149 
00150 /// int*Dims multiplication
00151 inline Dims operator*(int factor, const Dims& d)
00152 {
00153   return Dims(d.w()*factor, d.h()*factor);
00154 }
00155 
00156 /// double*Dims multiplication
00157 inline Dims operator*(double factor, const Dims& d)
00158 {
00159   return Dims(int(d.w()*factor), int(d.h()*factor));
00160 }
00161 
00162 /// Dims/int division
00163 inline Dims operator/(const Dims& d, int factor)
00164 {
00165   return Dims(d.w()/factor, d.h()/factor);
00166 }
00167 
00168 /// Dims/double division
00169 inline Dims operator/(const Dims& d, double factor)
00170 {
00171   return Dims(int(d.w()/factor), int(d.h()/factor));
00172 }
00173 
00174 // ######################################################################
00175 // ######################################################################
00176 // INLINE member functions for Dims:
00177 // ######################################################################
00178 // ######################################################################
00179 
00180 // ######################################################################
00181 inline Dims::Dims() throw() :
00182   ww(0), hh(0)
00183 {}
00184 
00185 // ######################################################################
00186 inline Dims::Dims(int width, int height) throw() :
00187   ww(width), hh(height)
00188 {
00189   ASSERT(ww >= 0 && hh >= 0);
00190 }
00191 
00192 // ######################################################################
00193 inline Dims::Dims(const Dims& other) throw() :
00194   ww(other.ww), hh(other.hh)
00195 {}
00196 
00197 // ######################################################################
00198 inline int Dims::w() const throw()
00199 { return ww; }
00200 
00201 // ######################################################################
00202 inline int Dims::h() const throw()
00203 { return hh; }
00204 
00205 // ######################################################################
00206 inline int Dims::sz() const throw()
00207 { return ww*hh; }
00208 
00209 // ######################################################################
00210 inline int Dims::max() const throw()
00211 { return (ww > hh) ? ww : hh; }
00212 
00213 // ######################################################################
00214 inline int Dims::min() const throw()
00215 { return (ww < hh) ? ww : hh; }
00216 
00217 // ######################################################################
00218 inline bool Dims::isEmpty() const throw()
00219 { return sz() == 0; }
00220 
00221 // ######################################################################
00222 inline bool Dims::isNonEmpty() const throw() { return sz() > 0; }
00223 
00224 // ######################################################################
00225 inline bool Dims::operator==(const Dims& x) const throw()
00226 { return ww == x.ww && hh == x.hh; }
00227 
00228 // ######################################################################
00229 inline bool Dims::operator!=(const Dims& x) const throw()
00230 { return ww != x.ww || hh != x.hh; }
00231 
00232 #endif
00233 
00234 // ######################################################################
00235 /* So things look consistent in everyone's emacs... */
00236 /* Local Variables: */
00237 /* indent-tabs-mode: nil */
00238 /* End: */
Generated on Sun May 8 08:40:49 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3