www/api/Magick++/Geometry_8cpp_source.html
| Magick++ 7.1.0 |
Geometry.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4 //
5 // Copyright @ 2014 ImageMagick Studio LLC, a non-profit organization
6 // dedicated to making software imaging solutions freely available.
7 //
8 // Geometry implementation
9 //
10
11 #define MAGICKCORE_IMPLEMENTATION 1
12 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
13
14 #include "Magick++/Include.h"
15 #include <string>
16 #include <ctype.h> // for isdigit
17 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
18 #include <strings.h>
19 #endif
20
21 #include "Magick++/Geometry.h"
22 #include "Magick++/Exception.h"
23
24 using namespace std;
25
26 MagickPPExport int Magick::operator ==(const Magick::Geometry& left_,
27const Magick::Geometry& right_)
28 {
29return((left_.aspect() == right_.aspect()) &&
30 (left_.fillArea() == right_.fillArea()) &&
31 (left_.greater() == right_.greater()) &&
32 (left_.height() == right_.height()) &&
33 (left_.isValid() == right_.isValid()) &&
34 (left_.less() == right_.less()) &&
35 (left_.limitPixels() == right_.limitPixels()) &&
36 (left_.percent() == right_.percent()) &&
37 (left_.width() == right_.width()) &&
38 (left_.xOff() == right_.xOff()) &&
39 (left_.yOff() == right_.yOff()));
40 }
41
42 MagickPPExport int Magick::operator !=(const Magick::Geometry& left_,
43const Magick::Geometry& right_)
44 {
45return(!(left_ == right_));
46 }
47
48 MagickPPExport int Magick::operator >(const Magick::Geometry& left_,
49const Magick::Geometry& right_)
50 {
51return(!(left_ < right_) && (left_ != right_));
52 }
53
54 MagickPPExport int Magick::operator <(const Magick::Geometry& left_,
55const Magick::Geometry& right_)
56 {
57return((left_.width()*left_.height()) < (right_.width()*right_.height()));
58 }
59
60 MagickPPExport int Magick::operator >=(const Magick::Geometry& left_,
61const Magick::Geometry& right_)
62 {
63return((left_ > right_) || (left_ == right_));
64 }
65
66 MagickPPExport int Magick::operator <=(const Magick::Geometry& left_,
67const Magick::Geometry& right_ )
68 {
69return((left_ < right_) || (left_ == right_));
70 }
71
72 Magick::Geometry::Geometry(void)
73 : _width(0),
74 _height(0),
75 _xOff(0),
76 _yOff(0),
77 _isValid(false),
78 _percent(false),
79 _aspect(false),
80 _greater(false),
81 _less(false),
82 _fillArea(false),
83 _limitPixels(false)
84 {
85 }
86
87 Magick::Geometry::Geometry(const char *geometry_)
88 : _width(0),
89 _height(0),
90 _xOff(0),
91 _yOff(0),
92 _isValid(false),
93 _percent(false),
94 _aspect(false),
95 _greater(false),
96 _less(false),
97 _fillArea(false),
98 _limitPixels(false)
99 {
100 *this=geometry_; // Use assignment operator
101 }
102
103 Magick::Geometry::Geometry(const Geometry &geometry_)
104 : _width(geometry_._width),
105 _height(geometry_._height),
106 _xOff(geometry_._xOff),
107 _yOff(geometry_._yOff),
108 _isValid(geometry_._isValid),
109 _percent(geometry_._percent),
110 _aspect(geometry_._aspect),
111 _greater(geometry_._greater),
112 _less(geometry_._less),
113 _fillArea(geometry_._fillArea),
114 _limitPixels(geometry_._limitPixels)
115 {
116 }
117
118 Magick::Geometry::Geometry(const std::string &geometry_)
119 : _width(0),
120 _height(0),
121 _xOff(0),
122 _yOff(0),
123 _isValid(false),
124 _percent(false),
125 _aspect(false),
126 _greater(false),
127 _less(false),
128 _fillArea(false),
129 _limitPixels(false)
130 {
131 *this=geometry_; // Use assignment operator
132 }
133
134 Magick::Geometry::Geometry(size_t width_,size_t height_,ssize_t xOff_,
135 ssize_t yOff_)
136 : _width(width_),
137 _height(height_),
138 _xOff(xOff_),
139 _yOff(yOff_),
140 _isValid(true),
141 _percent(false),
142 _aspect(false),
143 _greater(false),
144 _less(false),
145 _fillArea(false),
146 _limitPixels(false)
147 {
148 }
149
150 Magick::Geometry::~Geometry(void)
151 {
152 }
153
154 const Magick::Geometry& Magick::Geometry::operator=(const char *geometry_)
155 {
156 *this=std::string(geometry_);
157return(*this);
158 }
159
160 Magick::Geometry& Magick::Geometry::operator=(const Geometry &geometry_)
161 {
162// If not being set to ourself
163if (this != &geometry_)
164 {
165 _width=geometry_._width;
166 _height=geometry_._height;
167 _xOff=geometry_._xOff;
168 _yOff=geometry_._yOff;
169 _isValid=geometry_._isValid;
170 _percent=geometry_._percent;
171 _aspect=geometry_._aspect;
172 _greater=geometry_._greater;
173 _less=geometry_._less;
174 _fillArea=geometry_._fillArea;
175 _limitPixels=geometry_._limitPixels;
176 }
177return(*this);
178 }
179
180 const Magick::Geometry& Magick::Geometry::operator=(
181const std::string &geometry_)
182 {
183char
184 geom[MagickPathExtent];
185
186char
187 *pageptr;
188
189 ssize_t
190 flags,
191 x = 0,
192 y = 0;
193
194size_t
195 height_val=0,
196 width_val=0;
197
198// If argument does not start with digit, presume that it is a
199// page-size specification that needs to be converted to an
200// equivalent geometry specification using PostscriptGeometry()
201 (void) CopyMagickString(geom,geometry_.c_str(),MagickPathExtent);
202if (geom[0] != '-' && geom[0] != '+' && geom[0] != 'x' &&
203 !isdigit(static_cast<int>(geom[0])))
204 {
205 pageptr=GetPageGeometry(geom);
206if (pageptr != 0)
207 {
208 (void) CopyMagickString(geom,pageptr,MagickPathExtent);
209 pageptr=(char *) RelinquishMagickMemory(pageptr);
210 }
211 }
212
213 flags=GetGeometry(geom,&x,&y,&width_val,&height_val);
214
215if (flags == NoValue)
216 {
217// Total failure!
218 *this=Geometry();
219 isValid(false);
220return(*this);
221 }
222
223if ((flags & WidthValue) != 0)
224 {
225 _width=width_val;
226 isValid(true);
227 }
228
229if ((flags & HeightValue) != 0)
230 {
231 _height=height_val;
232 isValid(true);
233 }
234
235if ((flags & XValue) != 0)
236 {
237 _xOff=static_cast<ssize_t>(x);
238 isValid(true);
239 }
240
241if ((flags & YValue) != 0)
242 {
243 _yOff=static_cast<ssize_t>(y);
244 isValid(true);
245 }
246
247if ((flags & PercentValue) != 0)
248 _percent=true;
249
250if ((flags & AspectValue) != 0)
251 _aspect=true;
252
253if ((flags & LessValue) != 0)
254 _less=true;
255
256if ((flags & GreaterValue) != 0)
257 _greater=true;
258
259if ((flags & MinimumValue) != 0)
260 _fillArea=true;
261
262if ((flags & AreaValue) != 0)
263 _limitPixels=true;
264
265return(*this);
266 }
267
268 Magick::Geometry::operator std::string() const
269 {
270char
271 buffer[MagickPathExtent];
272
273 std::string
274 geometry;
275
276if (!isValid())
277throwExceptionExplicit(MagickCore::OptionError,
278"Invalid geometry argument");
279
280if (_width)
281 {
282 FormatLocaleString(buffer,MagickPathExtent,"%.20g",(double) _width);
283 geometry+=buffer;
284 }
285
286if (_height)
287 {
288 FormatLocaleString(buffer,MagickPathExtent,"%.20g",(double) _height);
289 geometry+='x';
290 geometry+=buffer;
291 }
292
293if (_xOff || _yOff)
294 {
295if (_xOff >= 0)
296 geometry+='+';
297
298 FormatLocaleString(buffer,MagickPathExtent,"%.20g",(double) _xOff);
299 geometry+=buffer;
300
301if (_yOff >= 0)
302 geometry+='+';
303
304 FormatLocaleString(buffer,MagickPathExtent,"%.20g",(double) _yOff);
305 geometry+=buffer;
306 }
307
308if (_percent)
309 geometry+='%';
310
311if (_aspect)
312 geometry+='!';
313
314if (_greater)
315 geometry+='>';
316
317if (_less)
318 geometry+='<';
319
320if (_fillArea)
321 geometry+='^';
322
323if (_limitPixels)
324 geometry+='@';
325
326return(geometry);
327 }
328
329 void Magick::Geometry::aspect(bool aspect_)
330 {
331 _aspect=aspect_;
332 }
333
334 bool Magick::Geometry::aspect(void) const
335 {
336return(_aspect);
337 }
338
339 void Magick::Geometry::fillArea(bool fillArea_)
340 {
341 _fillArea=fillArea_;
342 }
343
344 bool Magick::Geometry::fillArea(void) const
345 {
346return(_fillArea);
347 }
348
349 void Magick::Geometry::greater(bool greater_)
350 {
351 _greater=greater_;
352 }
353
354 bool Magick::Geometry::greater(void) const
355 {
356return(_greater);
357 }
358
359 void Magick::Geometry::height(size_t height_)
360 {
361 _height=height_;
362 }
363
364 size_t Magick::Geometry::height(void) const
365 {
366return(_height);
367 }
368
369 void Magick::Geometry::isValid(bool isValid_)
370 {
371 _isValid=isValid_;
372 }
373
374 bool Magick::Geometry::isValid(void) const
375 {
376return(_isValid);
377 }
378
379 void Magick::Geometry::less(bool less_)
380 {
381 _less=less_;
382 }
383
384 bool Magick::Geometry::less(void) const
385 {
386return(_less);
387 }
388
389 void Magick::Geometry::limitPixels(bool limitPixels_)
390 {
391 _limitPixels=limitPixels_;
392 }
393
394 bool Magick::Geometry::limitPixels(void) const
395 {
396return(_limitPixels);
397 }
398
399 void Magick::Geometry::width(size_t width_)
400 {
401 _width=width_;
402 isValid(true);
403 }
404
405 void Magick::Geometry::percent(bool percent_)
406 {
407 _percent = percent_;
408 }
409
410 bool Magick::Geometry::percent(void) const
411 {
412return(_percent);
413 }
414
415 size_t Magick::Geometry::width(void) const
416 {
417return(_width);
418 }
419
420 void Magick::Geometry::xOff(::ssize_t xOff_)
421 {
422 _xOff=xOff_;
423 }
424
425 ::ssize_t Magick::Geometry::xOff(void) const
426 {
427return(_xOff);
428 }
429
430 void Magick::Geometry::yOff(::ssize_t yOff_)
431 {
432 _yOff=yOff_;
433 }
434
435 ::ssize_t Magick::Geometry::yOff(void) const
436 {
437return(_yOff);
438 }
439
440 Magick::Geometry::Geometry(const MagickCore::RectangleInfo &rectangle_)
441 : _width(static_cast<size_t>(rectangle_.width)),
442 _height(static_cast<size_t>(rectangle_.height)),
443 _xOff(static_cast<ssize_t>(rectangle_.x)),
444 _yOff(static_cast<ssize_t>(rectangle_.y)),
445 _isValid(true),
446 _percent(false),
447 _aspect(false),
448 _greater(false),
449 _less(false),
450 _fillArea(false),
451 _limitPixels(false)
452 {
453 }
454
455 const Magick::Geometry& Magick::Geometry::operator=(
456const MagickCore::RectangleInfo &rectangle_)
457 {
458 _width=static_cast<size_t>(rectangle_.width),
459 _height=static_cast<size_t>(rectangle_.height),
460 _xOff=static_cast<ssize_t>(rectangle_.x),
461 _yOff=static_cast<ssize_t>(rectangle_.y),
462 _isValid=true;
463return(*this);
464 }
465
466 Magick::Geometry::operator MagickCore::RectangleInfo() const
467 {
468 RectangleInfo rectangle;
469 rectangle.width=_width;
470 rectangle.height=_height;
471 rectangle.x=_xOff;
472 rectangle.y=_yOff;
473return(rectangle);
474 }
475
476 MagickPPExport int Magick::operator ==(const Magick::Offset& left_,
477const Magick::Offset& right_)
478 {
479return((left_.x() == right_.x()) &&
480 (left_.y() == right_.y()));
481 }
482
483 MagickPPExport int Magick::operator !=(const Magick::Offset& left_,
484const Magick::Offset& right_)
485 {
486return(!(left_ == right_));
487 }
488
489 Magick::Offset::Offset(void)
490 : _x(0),
491 _y(0)
492 {
493 }
494
495 Magick::Offset::Offset(const char *offset_)
496 : _x(0),
497 _y(0)
498 {
499 *this=offset_; // Use assignment operator
500 }
501
502 Magick::Offset::Offset(const Offset &offset_)
503 : _x(offset_._x),
504 _y(offset_._y)
505 {
506 }
507
508 Magick::Offset::Offset(const std::string &offset_)
509 : _x(0),
510 _y(0)
511 {
512 *this=offset_; // Use assignment operator
513 }
514
515 Magick::Offset::Offset(ssize_t x_,ssize_t y_)
516 : _x(x_),
517 _y(y_)
518 {
519 }
520
521 Magick::Offset::~Offset(void)
522 {
523 }
524
525 const Magick::Offset& Magick::Offset::operator=(const char *offset_)
526 {
527 MagickCore::GeometryInfo
528 geometry_info;
529
530 MagickCore::MagickStatusType
531 flags;
532
533 flags=ParseGeometry(offset_,&geometry_info);
534 _x=geometry_info.rho;
535 _y=geometry_info.sigma;
536if ((flags & MagickCore::SigmaValue) == 0)
537 _y=_x;
538return(*this);
539 }
540
541 Magick::Offset& Magick::Offset::operator=(const Offset &offset_)
542 {
543// If not being set to ourself
544if (this != &offset_)
545 {
546 _x=offset_._x;
547 _y=offset_._y;
548 }
549return(*this);
550 }
551
552 const Magick::Offset& Magick::Offset::operator=(const std::string &offset_)
553 {
554 *this=offset_.c_str();
555return(*this);
556 }
557
558 ssize_t Magick::Offset::x(void) const
559 {
560return(_x);
561 }
562
563 ssize_t Magick::Offset::y(void) const
564 {
565return(_y);
566 }
567
568 Magick::Offset::operator MagickCore::OffsetInfo() const
569 {
570 OffsetInfo offset;
571 offset.x=_x;
572 offset.y=_y;
573return(offset);
574 }
575
576 MagickPPExport int Magick::operator ==(const Magick::Point& left_,
577const Magick::Point& right_)
578 {
579return((left_.x() == right_.x()) &&
580 (left_.y() == right_.y()));
581 }
582
583 MagickPPExport int Magick::operator !=(const Magick::Point& left_,
584const Magick::Point& right_)
585 {
586return(!(left_ == right_));
587 }
588
589 Magick::Point::Point(void)
590 : _x(0.0),
591 _y(0.0)
592 {
593 }
594
595 Magick::Point::Point(const char *point_)
596 : _x(0.0),
597 _y(0.0)
598 {
599 *this=point_; // Use assignment operator
600 }
601
602 Magick::Point::Point(const Point &point_)
603 : _x(point_._x),
604 _y(point_._y)
605 {
606 }
607
608 Magick::Point::Point(const std::string &point_)
609 : _x(0.0),
610 _y(0.0)
611 {
612 *this=point_; // Use assignment operator
613 }
614
615 Magick::Point::Point(double x_,double y_)
616 : _x(x_),
617 _y(y_)
618 {
619 }
620
621 Magick::Point::Point(double xy_)
622 : _x(xy_),
623 _y(xy_)
624 {
625 }
626
627 Magick::Point::~Point(void)
628 {
629 }
630
631 const Magick::Point& Magick::Point::operator=(const char *point_)
632 {
633 MagickCore::GeometryInfo
634 geometry_info;
635
636 MagickCore::MagickStatusType
637 flags;
638
639 flags=ParseGeometry(point_,&geometry_info);
640 _x=geometry_info.rho;
641 _y=geometry_info.sigma;
642if ((flags & MagickCore::SigmaValue) == 0)
643 _y=_x;
644return(*this);
645 }
646
647 const Magick::Point& Magick::Point::operator=(const double xy_)
648 {
649 _x=xy_;
650 _y=xy_;
651return(*this);
652 }
653
654 Magick::Point& Magick::Point::operator=(const Point &point_)
655 {
656// If not being set to ourself
657if (this != &point_)
658 {
659 _x=point_._x;
660 _y=point_._y;
661 }
662return(*this);
663 }
664
665 const Magick::Point& Magick::Point::operator=(const std::string &point_)
666 {
667 *this=point_.c_str();
668return(*this);
669 }
670
671 Magick::Point::operator std::string() const
672 {
673char
674 buffer[MagickPathExtent];
675
676string
677 point;
678
679if (_x < 0.0)
680 point+="-";
681else
682 point+="+";
683
684 FormatLocaleString(buffer,MagickPathExtent,"%.20g",_x);
685 point+=buffer;
686
687if (_y < 0.0)
688 point+="x-";
689else
690 point+="x+";
691
692 FormatLocaleString(buffer,MagickPathExtent,"%.20g",(double) _y);
693 point+=buffer;
694
695return(point);
696 }
697
698 bool Magick::Point::isValid(void) const
699 {
700return(_x > 0.0);
701 }
702
703 double Magick::Point::x(void) const
704 {
705return(_x);
706 }
707
708 double Magick::Point::y(void) const
709 {
710return(_y);
711 }
size_t width(void) const
Definition: Geometry.cpp:415
const Geometry & operator=(const char *geometry_)
Definition: Geometry.cpp:154
MagickPPExport int operator!=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:38
~Geometry(void)
Definition: Geometry.cpp:150
void width(size_t width_)
Definition: Geometry.cpp:399
void aspect(bool aspect_)
Definition: Geometry.cpp:329
Geometry()
Definition: Geometry.cpp:72
bool fillArea(void) const
Definition: Geometry.cpp:344
void height(size_t height_)
Definition: Geometry.cpp:359
Definition: Geometry.h:208
Point()
Definition: Geometry.cpp:589
bool isValid() const
Definition: Geometry.cpp:698
size_t height(void) const
Definition: Geometry.cpp:364
bool limitPixels(void) const
Definition: Geometry.cpp:394
STL namespace.
const Point & operator=(const char *point_)
Definition: Geometry.cpp:631
Offset()
Definition: Geometry.cpp:489
bool aspect(void) const
Definition: Geometry.cpp:334
MagickPPExport int operator<(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:50
void yOff(::ssize_t yOff_)
Definition: Geometry.cpp:430
bool percent(void) const
Definition: Geometry.cpp:410
ssize_t x(void) const
Definition: Geometry.cpp:558
MagickPPExport int operator<=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:72
MagickPPExport int operator>=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:66
~Point(void)
Definition: Geometry.cpp:627
bool less(void) const
Definition: Geometry.cpp:384
bool isValid(void) const
Definition: Geometry.cpp:374
#define MagickPPExport
Definition: Include.h:297
class MagickPPExport Geometry
Definition: Geometry.h:21
Definition: Geometry.h:151
void xOff(::ssize_t xOff_)
Definition: Geometry.cpp:420
ssize_t y(void) const
Definition: Geometry.cpp:563
const Offset & operator=(const char *offset_)
Definition: Geometry.cpp:525
void greater(bool greater_)
Definition: Geometry.cpp:349
Magick::throwExceptionExplicit
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
Definition: Exception.cpp:808
Definition: Geometry.h:37
::ssize_t xOff(void) const
Definition: Geometry.cpp:425
void fillArea(bool fillArea_)
Definition: Geometry.cpp:339
double x(void) const
Definition: Geometry.cpp:703
~Offset(void)
Definition: Geometry.cpp:521
void less(bool less_)
Definition: Geometry.cpp:379
::ssize_t yOff(void) const
Definition: Geometry.cpp:435
double y(void) const
Definition: Geometry.cpp:708
MagickPPExport int operator>(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:44
MagickPPExport int operator==(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:22
bool greater(void) const
Definition: Geometry.cpp:354
void percent(bool percent_)
Definition: Geometry.cpp:405
void isValid(bool isValid_)
Definition: Geometry.cpp:369
void limitPixels(bool limitPixels_)
Definition: Geometry.cpp:389