Powered by Pair ImageMagick logo
Image Magick
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

ImageMagick-6.1.1/Magick++/lib/Geometry.cpp

Go to the documentation of this file.
00001 // This may look like C code, but it is really -*- C++ -*- 00002 // 00003 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003 00004 // 00005 // Geometry implementation 00006 // 00007 00008 #define MAGICK_IMPLEMENTATION 1 00009 00010 #include "Magick++/Include.h" 00011 #include <string> 00012 #include <ctype.h> // for isdigit 00013 00014 using namespace std; 00015 00016 #include "Magick++/Geometry.h" 00017 #include "Magick++/Exception.h" 00018 00019 #define AbsoluteValue(x) ((x) < 0 ? -(x) : (x)) 00020 00021 int Magick::operator == ( const Magick::Geometry& left_, 00022 const Magick::Geometry& right_ ) 00023 { 00024 return ( 00025 ( left_.isValid() == right_.isValid() ) && 00026 ( left_.width() == right_.width() ) && 00027 ( left_.height() == right_.height() ) && 00028 ( left_.xOff() == right_.xOff() ) && 00029 ( left_.yOff() == right_.yOff() ) && 00030 ( left_.xNegative() == right_.xNegative() ) && 00031 ( left_.yNegative() == right_.yNegative() ) && 00032 ( left_.percent() == right_.percent() ) && 00033 ( left_.aspect() == right_.aspect() ) && 00034 ( left_.greater() == right_.greater() ) && 00035 ( left_.less() == right_.less() ) 00036 ); 00037 } 00038 int Magick::operator != ( const Magick::Geometry& left_, 00039 const Magick::Geometry& right_ ) 00040 { 00041 return ( ! (left_ == right_) ); 00042 } 00043 int Magick::operator > ( const Magick::Geometry& left_, 00044 const Magick::Geometry& right_ ) 00045 { 00046 return ( !( left_ < right_ ) && ( left_ != right_ ) ); 00047 } 00048 int Magick::operator < ( const Magick::Geometry& left_, 00049 const Magick::Geometry& right_ ) 00050 { 00051 return ( 00052 ( left_.width() * left_.height() ) 00053 < 00054 ( right_.width() * right_.height() ) 00055 ); 00056 } 00057 int Magick::operator >= ( const Magick::Geometry& left_, 00058 const Magick::Geometry& right_ ) 00059 { 00060 return ( ( left_ > right_ ) || ( left_ == right_ ) ); 00061 } 00062 int Magick::operator <= ( const Magick::Geometry& left_, 00063 const Magick::Geometry& right_ ) 00064 { 00065 return ( ( left_ < right_ ) || ( left_ == right_ ) ); 00066 } 00067 00068 // Construct using parameterized arguments 00069 Magick::Geometry::Geometry ( unsigned int width_, 00070 unsigned int height_, 00071 unsigned int xOff_, 00072 unsigned int yOff_, 00073 bool xNegative_, 00074 bool yNegative_ ) 00075 : _width( width_ ), 00076 _height( height_ ), 00077 _xOff( xOff_ ), 00078 _yOff( yOff_ ), 00079 _xNegative( xNegative_ ), 00080 _yNegative( yNegative_ ), 00081 _isValid( true ), 00082 _percent( false ), 00083 _aspect( false ), 00084 _greater( false ), 00085 _less( false ) 00086 { 00087 } 00088 00089 // Assignment from C++ string 00090 Magick::Geometry::Geometry ( const std::string &geometry_ ) 00091 : _width( 0 ), 00092 _height( 0 ), 00093 _xOff( 0 ), 00094 _yOff( 0 ), 00095 _xNegative( false ), 00096 _yNegative( false ), 00097 _isValid( false ), 00098 _percent( false ), 00099 _aspect( false ), 00100 _greater( false ), 00101 _less( false ) 00102 { 00103 *this = geometry_; // Use assignment operator 00104 } 00105 00106 00107 // Assignment from C character string 00108 Magick::Geometry::Geometry ( const char *geometry_ ) 00109 : _width( 0 ), 00110 _height( 0 ), 00111 _xOff( 0 ), 00112 _yOff( 0 ), 00113 _xNegative( false ), 00114 _yNegative( false ), 00115 _isValid( false ), 00116 _percent( false ), 00117 _aspect( false ), 00118 _greater( false ), 00119 _less( false ) 00120 { 00121 *this = geometry_; // Use assignment operator 00122 } 00123 00124 // Copy constructor 00125 Magick::Geometry::Geometry ( const Geometry &geometry_ ) 00126 : _width( geometry_._width ), 00127 _height( geometry_._height ), 00128 _xOff( geometry_._xOff ), 00129 _yOff( geometry_._yOff ), 00130 _xNegative( geometry_._xNegative ), 00131 _yNegative( geometry_._yNegative ), 00132 _isValid ( geometry_._isValid ), 00133 _percent( geometry_._percent ), 00134 _aspect( geometry_._aspect ), 00135 _greater( geometry_._greater ), 00136 _less( geometry_._less ) 00137 { 00138 } 00139 00140 // Default constructor 00141 Magick::Geometry::Geometry ( void ) 00142 : _width( 0 ), 00143 _height( 0 ), 00144 _xOff( 0 ), 00145 _yOff( 0 ), 00146 _xNegative( false ), 00147 _yNegative( false ), 00148 _isValid ( false ), 00149 _percent( false ), 00150 _aspect( false ), 00151 _greater( false ), 00152 _less( false ) 00153 { 00154 } 00155 00156 /* virtual */ Magick::Geometry::~Geometry ( void ) 00157 { 00158 // Nothing to do 00159 } 00160 00161 Magick::Geometry& Magick::Geometry::operator = ( const Geometry& geometry_ ) 00162 { 00163 // If not being set to ourself 00164 if ( this != &geometry_ ) 00165 { 00166 _width = geometry_._width; 00167 _height = geometry_._height; 00168 _xOff = geometry_._xOff; 00169 _yOff = geometry_._yOff; 00170 _xNegative = geometry_._xNegative; 00171 _yNegative = geometry_._yNegative; 00172 _isValid = geometry_._isValid; 00173 _percent = geometry_._percent; 00174 _aspect = geometry_._aspect; 00175 _greater = geometry_._greater; 00176 _less = geometry_._less; 00177 } 00178 return *this; 00179 } 00180 00181 // Set value via geometry string 00182 /* virtual */ const Magick::Geometry& 00183 Magick::Geometry::operator = ( const std::string &geometry_ ) 00184 { 00185 char 00186 geom[MaxTextExtent]; 00187 00188 // If argument does not start with digit, presume that it is a 00189 // page-size specification that needs to be converted to an 00190 // equivalent geometry specification using PostscriptGeometry() 00191 strcpy(geom,geometry_.c_str()); 00192 if ( geom[0] != '-' && 00193 geom[0] != '+' && 00194 geom[0] != 'x' && 00195 !isdigit(static_cast<int>(geom[0]))) 00196 { 00197 char *pageptr = GetPageGeometry( geom ); 00198 if ( pageptr != 0 ) 00199 { 00200 strcpy(geom,pageptr); 00201 pageptr=(char *) RelinquishMagickMemory( pageptr ); 00202 } 00203 } 00204 00205 long x = 0; 00206 long y = 0; 00207 unsigned long width_val = 0; 00208 unsigned long height_val = 0; 00209 int flags = GetGeometry (geom, &x, &y, &width_val, &height_val ); 00210 00211 if (flags == NoValue) 00212 { 00213 // Total failure! 00214 *this=Geometry(); 00215 isValid( false ); 00216 return *this; 00217 } 00218 00219 if ( ( flags & WidthValue ) != 0 ) 00220 { 00221 _width = width_val; 00222 isValid( true ); 00223 } 00224 00225 if ( ( flags & HeightValue ) != 0 ) 00226 _height = height_val; 00227 00228 if ( ( flags & XValue ) != 0 ) 00229 { 00230 _xOff = static_cast<unsigned int>(AbsoluteValue(x)); 00231 isValid( true ); 00232 } 00233 00234 if ( ( flags & YValue ) != 0 ) 00235 { 00236 _yOff = static_cast<unsigned int>(AbsoluteValue(y)); 00237 isValid( true ); 00238 } 00239 00240 if ( ( flags & XNegative ) != 0 ) 00241 _xNegative = true; 00242 00243 if ( ( flags & YNegative ) != 0 ) 00244 _yNegative = true; 00245 00246 if ( ( flags & PercentValue ) != 0 ) 00247 _percent = true; 00248 00249 if ( ( flags & AspectValue ) != 0 ) 00250 _aspect = true; 00251 00252 if ( ( flags & LessValue ) != 0 ) 00253 _less = true; 00254 00255 if ( ( flags & GreaterValue ) != 0 ) 00256 _greater = true; 00257 00258 return *this; 00259 } 00260 00261 00262 // Set value via geometry C string 00263 /* virtual */ const Magick::Geometry& Magick::Geometry::operator = ( const char * geometry_ ) 00264 { 00265 *this = std::string(geometry_); 00266 return *this; 00267 } 00268 00269 // Return geometry string 00270 Magick::Geometry::operator std::string() const 00271 { 00272 if (!isValid()) 00273 { 00274 throwExceptionExplicit( OptionError, "Invalid geometry argument" ); 00275 } 00276 00277 string geometry; 00278 char buffer[32]; 00279 00280 if ( _width ) 00281 { 00282 FormatString( buffer, "%u", _width ); 00283 geometry += buffer; 00284 } 00285 00286 if ( _width && _height ) 00287 { 00288 FormatString( buffer, "%u", _height); 00289 geometry += 'x'; 00290 geometry += buffer; 00291 } 00292 00293 if ( _xOff || _yOff ) 00294 { 00295 if ( _xNegative ) 00296 geometry += '-'; 00297 else 00298 geometry += '+'; 00299 00300 FormatString( buffer, "%u", _xOff); 00301 geometry += buffer; 00302 00303 if ( _yNegative ) 00304 geometry += '-'; 00305 else 00306 geometry += '+'; 00307 00308 FormatString( buffer, "%u", _yOff); 00309 geometry += buffer; 00310 } 00311 00312 if ( _percent ) 00313 geometry += '%'; 00314 00315 if ( _aspect ) 00316 geometry += '!'; 00317 00318 if ( _greater ) 00319 geometry += '>'; 00320 00321 if ( _less ) 00322 geometry += '<'; 00323 00324 return geometry; 00325 } 00326 00327 // Construct from RectangleInfo 00328 Magick::Geometry::Geometry ( const MagickLib::RectangleInfo &rectangle_ ) 00329 : _width(static_cast<unsigned int>(rectangle_.width)), 00330 _height(static_cast<unsigned int>(rectangle_.height)), 00331 _xOff(static_cast<unsigned int>(AbsoluteValue(rectangle_.x))), 00332 _yOff(static_cast<unsigned int>(AbsoluteValue(rectangle_.y))), 00333 _xNegative(rectangle_.x < 0 ? true : false), 00334 _yNegative(rectangle_.y < 0 ? true : false), 00335 _isValid(true), 00336 _percent(false), 00337 _aspect(false), 00338 _greater(false), 00339 _less(false) 00340 { 00341 } 00342 00343 // Return an ImageMagick RectangleInfo struct 00344 Magick::Geometry::operator MagickLib::RectangleInfo() const 00345 { 00346 RectangleInfo rectangle; 00347 rectangle.width = _width; 00348 rectangle.height = _height; 00349 _xNegative ? rectangle.x = static_cast<long>(0-_xOff) : rectangle.x = static_cast<long>(_xOff); 00350 _yNegative ? rectangle.y = static_cast<long>(0-_yOff) : rectangle.y = static_cast<long>(_yOff); 00351 return rectangle; 00352 }

Generated on Mon Oct 25 13:41:10 2004 for ImageMagick by doxygen 1.3.7
ImageMagick Copyright © 2004, ImageMagick Studio LLC