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