CVD 0.8
cvd/image_ref.h
00001 /*
00002     This file is part of the CVD Library.
00003 
00004     Copyright (C) 2005 The Authors
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc.,
00019     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 //-*- c++ -*-
00023 //                                                                      //
00024 //  CVD::image.h                                                        //
00025 //                                                                      //
00026 //  Definitions for of template classes CVD::ImageRef and CVD::Image    //
00027 //                                                                      //
00028 //  derived from IPRS_* developed by Tom Drummond                       //
00029 //                                                                      //
00031 
00032 #ifndef __CVD_IMAGE_REF_H__
00033 #define __CVD_IMAGE_REF_H__
00034 
00035 #include <iostream>
00036 #include <cctype>
00037 #include <cvd/exceptions.h>
00038 
00039 namespace CVD {
00040 
00042 
00044 // CVD::ImageRef                    //
00047 class ImageRef
00048 {
00049 public:
00050 
00051     //Construction
00053     inline ImageRef();
00057     inline ImageRef(int xp, int yp);
00060     inline ImageRef(std::istream& is);
00061 
00062     //Iteration
00063 
00068     inline bool next(const ImageRef& max);
00073     inline bool prev(const ImageRef& max);
00080     inline bool next(const ImageRef& min, const ImageRef& max);
00087     inline bool prev(const ImageRef& min, const ImageRef& max);
00088 
00090     inline void home();
00094     inline void end(const ImageRef& size);
00095 
00096 
00097     //Operators
00098 
00101     inline ImageRef&    operator=(const ImageRef& ref);
00104     inline bool         operator==(const ImageRef& ref) const;
00107     inline bool         operator!=(const ImageRef& ref) const;
00109   inline ImageRef  operator-() const;
00112   inline ImageRef&  operator*=(const double scale);
00115     inline ImageRef&    operator/=(const double scale);
00118     inline ImageRef&    operator+=(const ImageRef rhs);
00121     inline ImageRef&    operator-=(const ImageRef rhs);
00124     inline ImageRef     operator*(const double scale) const;
00127     inline ImageRef     operator/(const double scale) const;
00130     inline ImageRef     operator+(const ImageRef rhs) const;
00133     inline ImageRef     operator-(const ImageRef rhs) const;
00136     inline ImageRef&    operator<<=(int i);
00139     inline ImageRef&    operator>>=(int i);
00142     inline ImageRef     operator>>(int i) const;
00145     inline ImageRef     operator<<(int i) const;
00151   inline bool operator<(const ImageRef & other) const;
00155   inline bool operator>(const ImageRef & other) const;
00156 
00158     inline unsigned int     mag_squared() const;
00159 
00161     inline int area() const;
00162 
00164     inline ImageRef dot_times(const ImageRef &ref) const;
00165 
00167     inline int& operator[](int i);
00168 
00170     inline int operator[](int i) const;
00171 
00172     //Why do these exist?
00174     inline ImageRef shiftl(int i) const;
00176     inline ImageRef shiftr(int i) const;
00177 
00178     // and now the data members (which are public!)
00179     int x; 
00180     int y; 
00181 
00182 };
00183 
00188 inline ImageRef operator*(const int scale, const ImageRef&  ref);
00189 
00190 namespace Exceptions
00191 {
00193   struct BadSubscript: public CVD::Exceptions::All {BadSubscript(){};};
00194 }
00195 
00196 
00197 #include <cvd/internal/image_ref_implementation.hh>
00198 
00199 // Streams stuff for ImageRef class //
00200 
00205 inline std::ostream& operator<<(std::ostream& os, const ImageRef& ref)
00206 {
00207     return os << "[" << ref.x << " " << ref.y << "]";
00208 }
00209 
00212 inline std::istream& operator>>(std::istream& is, ImageRef& ref)
00213 {
00214     //Full parsing for ImageRefs, to allow it to accept the
00215     //output produced above, as well as the older (x,y) format
00216     is >> std::ws;
00217 
00218     unsigned char c = is.get();
00219 
00220     if(is.eof())
00221         return is;
00222 
00223     if(c == '(' )
00224     {
00225         is >> std::ws >> ref.x >> std::ws;
00226 
00227         if(is.get() != ',')
00228             goto bad;
00229 
00230         is >> std::ws >> ref.y >> std::ws;
00231 
00232         if(is.get() != ')')
00233             goto bad;
00234     }
00235     else if(c == '[' )
00236     {
00237         is >> std::ws >> ref.x >> std::ws >> ref.y >> std::ws;
00238         if(is.get() != ']')
00239             goto bad;
00240     }
00241     else if(isdigit(c))
00242     {
00243         is.unget();
00244         is >> ref.x >> ref.y;
00245     }
00246     else
00247         goto bad;
00248 
00249     return is;
00250 
00251     bad:
00252     is.setstate(std::ios_base::badbit);
00253 
00254     return is;
00255 }
00256 
00259 const ImageRef ImageRef_zero(0, 0);
00260 
00261 
00262 } //namespace CVD
00263 
00264 
00265 #endif