]> granicus.if.org Git - imagemagick/blob - Magick++/lib/Image.cpp
(no commit message)
[imagemagick] / Magick++ / lib / Image.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 // Implementation of Image
6 //
7
8 #define MAGICKCORE_IMPLEMENTATION  1
9 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
10
11 #include <cstdlib>
12 #include <string>
13 #include <string.h>
14 #include <errno.h>
15 #include <math.h>
16 #include "Magick++/Include.h"
17
18 using namespace std;
19
20 #include "Magick++/Image.h"
21 #include "Magick++/Functions.h"
22 #include "Magick++/Pixels.h"
23 #include "Magick++/Options.h"
24 #include "Magick++/ImageRef.h"
25
26 #define AbsoluteValue(x)  ((x) < 0 ? -(x) : (x))
27 #define DegreesToRadians(x)  (MagickPI*(x)/180.0)
28
29 MagickPPExport const char *Magick::borderGeometryDefault = "6x6+0+0";
30 MagickPPExport const char *Magick::frameGeometryDefault  = "25x25+6+6";
31 MagickPPExport const char *Magick::raiseGeometryDefault  = "6x6+0+0";
32
33 static bool magick_initialized=false;
34
35 //
36 // Explicit template instantiations
37 //
38
39 //
40 // Friend functions to compare Image objects
41 //
42
43 MagickPPExport int Magick::operator == ( const Magick::Image& left_,
44                                         const Magick::Image& right_ )
45 {
46   // If image pixels and signature are the same, then the image is identical
47   return ( ( left_.rows() == right_.rows() ) &&
48            ( left_.columns() == right_.columns() ) &&
49            ( left_.signature() == right_.signature() )
50            );
51 }
52 MagickPPExport int Magick::operator != ( const Magick::Image& left_,
53                                         const Magick::Image& right_ )
54 {
55   return ( ! (left_ == right_) );
56 }
57 MagickPPExport int Magick::operator >  ( const Magick::Image& left_,
58                                         const Magick::Image& right_ )
59 {
60   return ( !( left_ < right_ ) && ( left_ != right_ ) );
61 }
62 MagickPPExport int Magick::operator <  ( const Magick::Image& left_,
63                                         const Magick::Image& right_ )
64 {
65   // If image pixels are less, then image is smaller
66   return ( ( left_.rows() * left_.columns() ) <
67            ( right_.rows() * right_.columns() )
68            );
69 }
70 MagickPPExport int Magick::operator >= ( const Magick::Image& left_,
71                                         const Magick::Image& right_ )
72 {
73   return ( ( left_ > right_ ) || ( left_ == right_ ) );
74 }
75 MagickPPExport int Magick::operator <= ( const Magick::Image& left_,
76                                         const Magick::Image& right_ )
77 {
78   return ( ( left_ < right_ ) || ( left_ == right_ ) );
79 }
80
81 //
82 // Image object implementation
83 //
84
85 // Construct from image file or image specification
86 Magick::Image::Image( const std::string &imageSpec_ )
87   : _imgRef(new ImageRef)
88 {
89   try
90     {
91       // Initialize, Allocate and Read images
92       read( imageSpec_ );
93     }
94   catch ( const Warning & /*warning_*/ )
95     {
96       // FIXME: need a way to report warnings in constructor
97     }
98   catch ( const Error & /*error_*/ )
99     {
100       // Release resources
101       delete _imgRef;
102       throw;
103     }
104 }
105
106 // Construct a blank image canvas of specified size and color
107 Magick::Image::Image( const Geometry &size_,
108                       const Color &color_ )
109   : _imgRef(new ImageRef)
110 {
111   // xc: prefix specifies an X11 color string
112   std::string imageSpec("xc:");
113   imageSpec += color_;
114
115   try
116     {
117       // Set image size
118       size( size_ );
119
120       // Initialize, Allocate and Read images
121       read( imageSpec );
122     }
123   catch ( const Warning & /*warning_*/ )
124     {
125       // FIXME: need a way to report warnings in constructor
126     }
127   catch ( const Error & /*error_*/ )
128     {
129       // Release resources
130       delete _imgRef;
131       throw;
132     }
133 }
134
135 // Construct Image from in-memory BLOB
136 Magick::Image::Image ( const Blob &blob_ )
137   : _imgRef(new ImageRef)
138 {
139   try
140     {
141       // Initialize, Allocate and Read images
142       read( blob_ );
143     }
144   catch ( const Warning & /*warning_*/ )
145     {
146       // FIXME: need a way to report warnings in constructor
147     }
148   catch ( const Error & /*error_*/ )
149     {
150       // Release resources
151       delete _imgRef;
152       throw;
153     }
154 }
155
156 // Construct Image of specified size from in-memory BLOB
157 Magick::Image::Image ( const Blob &blob_,
158                        const Geometry &size_ )
159   : _imgRef(new ImageRef)
160 {
161   try
162     {
163       // Read from Blob
164       read( blob_, size_ );
165     }
166   catch ( const Warning & /*warning_*/ )
167     {
168       // FIXME: need a way to report warnings in constructor
169     }
170   catch ( const Error & /*error_*/ )
171     {
172       // Release resources
173       delete _imgRef;
174       throw;
175     }
176 }
177
178 // Construct Image of specified size and depth from in-memory BLOB
179 Magick::Image::Image ( const Blob &blob_,
180                        const Geometry &size_,
181                        const size_t depth_ )
182   : _imgRef(new ImageRef)
183 {
184   try
185     {
186       // Read from Blob
187       read( blob_, size_, depth_ );
188     }
189   catch ( const Warning & /*warning_*/ )
190     {
191       // FIXME: need a way to report warnings in constructor
192     }
193   catch ( const Error & /*error_*/ )
194     {
195       // Release resources
196       delete _imgRef;
197       throw;
198     }
199 }
200
201 // Construct Image of specified size, depth, and format from in-memory BLOB
202 Magick::Image::Image ( const Blob &blob_,
203                        const Geometry &size_,
204                        const size_t depth_,
205                        const std::string &magick_ )
206   : _imgRef(new ImageRef)
207 {
208   try
209     {
210       // Read from Blob
211       read( blob_, size_, depth_, magick_ );
212     }
213   catch ( const Warning & /*warning_*/ )
214     {
215       // FIXME: need a way to report warnings in constructor
216     }
217   catch ( const Error & /*error_*/ )
218     {
219       // Release resources
220       delete _imgRef;
221       throw;
222     }
223 }
224
225 // Construct Image of specified size, and format from in-memory BLOB
226 Magick::Image::Image ( const Blob &blob_,
227                        const Geometry &size_,
228                        const std::string &magick_ )
229   : _imgRef(new ImageRef)
230 {
231   try
232     {
233       // Read from Blob
234       read( blob_, size_, magick_ );
235     }
236   catch ( const Warning & /*warning_*/ )
237     {
238       // FIXME: need a way to report warnings in constructor
239     }
240   catch ( const Error & /*error_*/ )
241     {
242       // Release resources
243       delete _imgRef;
244       throw;
245     }
246 }
247
248 // Construct an image based on an array of raw pixels, of specified
249 // type and mapping, in memory
250 Magick::Image::Image ( const size_t width_,
251                        const size_t height_,
252                        const std::string &map_,
253                        const StorageType type_,
254                        const void *pixels_ )
255   : _imgRef(new ImageRef)
256 {
257   try
258     {
259       read( width_, height_, map_.c_str(), type_, pixels_ );
260     }
261   catch ( const Warning & /*warning_*/ )
262     {
263       // FIXME: need a way to report warnings in constructor
264     }
265   catch ( const Error & /*error_*/ )
266     {
267       // Release resources
268       delete _imgRef;
269       throw;
270     }
271 }
272
273 // Default constructor
274 Magick::Image::Image( void )
275   : _imgRef(new ImageRef)
276 {
277 }
278
279 // Destructor
280 /* virtual */
281 Magick::Image::~Image()
282 {
283   bool doDelete = false;
284   {
285     Lock( &_imgRef->_mutexLock );
286     if ( --_imgRef->_refCount == 0 )
287       doDelete = true;
288   }
289
290   if ( doDelete )
291     {
292       delete _imgRef;
293     }
294   _imgRef = 0;
295 }
296
297 // Adaptive-blur image
298 void Magick::Image::adaptiveBlur( const double radius_, const double sigma_ )
299 {
300   ExceptionInfo exceptionInfo;
301   GetExceptionInfo( &exceptionInfo );
302   MagickCore::Image* newImage =
303     AdaptiveBlurImage( image(), radius_, sigma_, &exceptionInfo);
304   replaceImage( newImage );
305   throwException( exceptionInfo );
306   (void) DestroyExceptionInfo( &exceptionInfo );
307 }
308
309 // Local adaptive threshold image
310 // http://www.dai.ed.ac.uk/HIPR2/adpthrsh.htm
311 // Width x height define the size of the pixel neighborhood
312 // offset = constant to subtract from pixel neighborhood mean
313 void Magick::Image::adaptiveThreshold ( const size_t width_,
314                                         const size_t height_,
315                                         const ssize_t offset_ )
316 {
317   ExceptionInfo exceptionInfo;
318   GetExceptionInfo( &exceptionInfo );
319   MagickCore::Image* newImage =
320     AdaptiveThresholdImage( constImage(), width_, height_, offset_, &exceptionInfo );
321   replaceImage( newImage );
322   throwException( exceptionInfo );
323   (void) DestroyExceptionInfo( &exceptionInfo );
324 }
325
326 // Add noise to image
327 void Magick::Image::addNoise( const NoiseType noiseType_ )
328 {
329   ExceptionInfo exceptionInfo;
330   GetExceptionInfo( &exceptionInfo );
331   MagickCore::Image* newImage =
332     AddNoiseImage ( image(),
333                     noiseType_, 1.0,
334                     &exceptionInfo );
335   replaceImage( newImage );
336   throwException( exceptionInfo );
337   (void) DestroyExceptionInfo( &exceptionInfo );
338 }
339
340 void Magick::Image::addNoiseChannel( const ChannelType channel_,
341                                      const NoiseType noiseType_ )
342 {
343   ExceptionInfo exceptionInfo;
344   GetExceptionInfo( &exceptionInfo );
345   ChannelType channel_mask = SetPixelChannelMask( image(), channel_);
346   MagickCore::Image* newImage =
347     AddNoiseImage ( image(),
348                            noiseType_, 1.0,
349                            &exceptionInfo );
350   (void) SetPixelChannelMapMask( image(), channel_mask );
351   replaceImage( newImage );
352   throwException( exceptionInfo );
353   (void) DestroyExceptionInfo( &exceptionInfo );
354 }
355
356 // Affine Transform image
357 void Magick::Image::affineTransform ( const DrawableAffine &affine_ )
358 {
359   ExceptionInfo exceptionInfo;
360   GetExceptionInfo( &exceptionInfo );
361   
362   AffineMatrix _affine;
363   _affine.sx = affine_.sx();
364   _affine.sy = affine_.sy();
365   _affine.rx = affine_.rx();
366   _affine.ry = affine_.ry();
367   _affine.tx = affine_.tx();  
368   _affine.ty = affine_.ty();
369   
370   MagickCore::Image* newImage =
371     AffineTransformImage( image(), &_affine, &exceptionInfo);     
372   replaceImage( newImage );
373   throwException( exceptionInfo );
374   (void) DestroyExceptionInfo( &exceptionInfo );
375 }
376
377 // Annotate using specified text, and placement location
378 void Magick::Image::annotate ( const std::string &text_,
379                                const Geometry &location_ )
380 {
381   annotate ( text_, location_,  NorthWestGravity, 0.0 );
382 }
383 // Annotate using specified text, bounding area, and placement gravity
384 void Magick::Image::annotate ( const std::string &text_,
385                                const Geometry &boundingArea_,
386                                const GravityType gravity_ )
387 {
388   annotate ( text_, boundingArea_, gravity_, 0.0 );
389 }
390 // Annotate with text using specified text, bounding area, placement
391 // gravity, and rotation.
392 void Magick::Image::annotate ( const std::string &text_,
393                                const Geometry &boundingArea_,
394                                const GravityType gravity_,
395                                const double degrees_ )
396 {
397   modifyImage();
398
399   DrawInfo *drawInfo
400     = options()->drawInfo();
401   
402   drawInfo->text = const_cast<char *>(text_.c_str());
403
404   char boundingArea[MaxTextExtent];
405
406   drawInfo->geometry = 0;
407   if ( boundingArea_.isValid() ){
408     if ( boundingArea_.width() == 0 || boundingArea_.height() == 0 )
409       {
410         FormatLocaleString( boundingArea, MaxTextExtent, "%+.20g%+.20g",
411           (double) boundingArea_.xOff(), (double) boundingArea_.yOff() );
412       }
413     else
414       {
415         (void) CopyMagickString( boundingArea, string(boundingArea_).c_str(),
416           MaxTextExtent);
417       }
418     drawInfo->geometry = boundingArea;
419   }
420
421   drawInfo->gravity = gravity_;
422
423   AffineMatrix oaffine = drawInfo->affine;
424   if ( degrees_ != 0.0)
425     {
426         AffineMatrix affine;
427         affine.sx=1.0;
428         affine.rx=0.0;
429         affine.ry=0.0;
430         affine.sy=1.0;
431         affine.tx=0.0;
432         affine.ty=0.0;
433
434         AffineMatrix current = drawInfo->affine;
435         affine.sx=cos(DegreesToRadians(fmod(degrees_,360.0)));
436         affine.rx=sin(DegreesToRadians(fmod(degrees_,360.0)));
437         affine.ry=(-sin(DegreesToRadians(fmod(degrees_,360.0))));
438         affine.sy=cos(DegreesToRadians(fmod(degrees_,360.0)));
439
440         drawInfo->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
441         drawInfo->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
442         drawInfo->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
443         drawInfo->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
444         drawInfo->affine.tx=current.sx*affine.tx+current.ry*affine.ty
445           +current.tx;
446     }
447
448   ExceptionInfo exceptionInfo;
449   GetExceptionInfo( &exceptionInfo );
450   AnnotateImage( image(), drawInfo, &exceptionInfo );
451
452   // Restore original values
453   drawInfo->affine = oaffine;
454   drawInfo->text = 0;
455   drawInfo->geometry = 0;
456
457   throwException( exceptionInfo );
458   (void) DestroyExceptionInfo( &exceptionInfo );
459 }
460 // Annotate with text (bounding area is entire image) and placement gravity.
461 void Magick::Image::annotate ( const std::string &text_,
462                                const GravityType gravity_ )
463 {
464   modifyImage();
465
466   DrawInfo *drawInfo
467     = options()->drawInfo();
468
469   drawInfo->text = const_cast<char *>(text_.c_str());
470
471   drawInfo->gravity = gravity_;
472
473   ExceptionInfo exceptionInfo;
474   GetExceptionInfo( &exceptionInfo );
475   AnnotateImage( image(), drawInfo, &exceptionInfo );
476
477   drawInfo->gravity = NorthWestGravity;
478   drawInfo->text = 0;
479
480   throwException( exceptionInfo );
481   (void) DestroyExceptionInfo( &exceptionInfo );
482 }
483
484 // Blur image
485 void Magick::Image::blur( const double radius_, const double sigma_ )
486 {
487   ExceptionInfo exceptionInfo;
488   GetExceptionInfo( &exceptionInfo );
489   MagickCore::Image* newImage =
490     BlurImage( image(), radius_, sigma_, &exceptionInfo);
491   replaceImage( newImage );
492   throwException( exceptionInfo );
493   (void) DestroyExceptionInfo( &exceptionInfo );
494 }
495
496 void Magick::Image::blurChannel( const ChannelType channel_,
497                                  const double radius_, const double sigma_ )
498 {
499   ExceptionInfo exceptionInfo;
500   GetExceptionInfo( &exceptionInfo );
501   ChannelType channel_mask = SetPixelChannelMask( image(), channel_ );
502   MagickCore::Image* newImage =
503     BlurImage( image(), radius_, sigma_, &exceptionInfo);
504   (void) SetPixelChannelMapMask( image(), channel_mask );
505   replaceImage( newImage );
506   throwException( exceptionInfo );
507   (void) DestroyExceptionInfo( &exceptionInfo );
508 }
509
510 // Add border to image
511 // Only uses width & height
512 void Magick::Image::border( const Geometry &geometry_ )
513 {
514   RectangleInfo borderInfo = geometry_;
515   ExceptionInfo exceptionInfo;
516   GetExceptionInfo( &exceptionInfo );
517   MagickCore::Image* newImage =
518     BorderImage( image(), &borderInfo, image()->compose, &exceptionInfo);
519   replaceImage( newImage );
520   throwException( exceptionInfo );
521   (void) DestroyExceptionInfo( &exceptionInfo );
522 }
523
524 // Extract channel from image
525 void Magick::Image::channel ( const ChannelType channel_ )
526 {
527   modifyImage();
528   ExceptionInfo exceptionInfo;
529   GetExceptionInfo( &exceptionInfo );
530   MagickCore::Image* newImage =
531     SeparateImage( image(), channel_, &exceptionInfo);
532   replaceImage( newImage );
533   throwException( exceptionInfo );
534   (void) DestroyExceptionInfo( &exceptionInfo );
535 }
536
537 // Set or obtain modulus channel depth
538 void Magick::Image::channelDepth ( const size_t depth_ )
539 {
540   modifyImage();
541   ExceptionInfo exceptionInfo;
542   GetExceptionInfo( &exceptionInfo );
543   SetImageDepth( image(), depth_, &exceptionInfo);
544   throwException( exceptionInfo );
545   (void) DestroyExceptionInfo( &exceptionInfo );
546 }
547 size_t Magick::Image::channelDepth ( )
548 {
549   size_t channel_depth;
550
551   ExceptionInfo exceptionInfo;
552   GetExceptionInfo( &exceptionInfo );
553   channel_depth=GetImageDepth( constImage(), &exceptionInfo );
554   throwException( exceptionInfo );
555   (void) DestroyExceptionInfo( &exceptionInfo );
556   return channel_depth;
557 }
558
559
560 // Charcoal-effect image
561 void Magick::Image::charcoal( const double radius_, const double sigma_ )
562 {
563   ExceptionInfo exceptionInfo;
564   GetExceptionInfo( &exceptionInfo );
565   MagickCore::Image* newImage =
566     CharcoalImage( image(), radius_, sigma_, &exceptionInfo );
567   replaceImage( newImage );
568   throwException( exceptionInfo );
569   (void) DestroyExceptionInfo( &exceptionInfo );
570 }
571
572 // Chop image
573 void Magick::Image::chop( const Geometry &geometry_ )
574 {
575   RectangleInfo chopInfo = geometry_;
576   ExceptionInfo exceptionInfo;
577   GetExceptionInfo( &exceptionInfo );
578   MagickCore::Image* newImage =
579     ChopImage( image(), &chopInfo, &exceptionInfo);
580   replaceImage( newImage );
581   throwException( exceptionInfo );
582   (void) DestroyExceptionInfo( &exceptionInfo );
583 }
584
585 // contains one or more color corrections and applies the correction to the
586 // image.
587 void Magick::Image::cdl ( const std::string &cdl_ )
588 {
589   modifyImage();
590   ExceptionInfo exceptionInfo;
591   GetExceptionInfo( &exceptionInfo );
592   (void) ColorDecisionListImage( image(), cdl_.c_str(), &exceptionInfo );
593   throwException( exceptionInfo );
594   (void) DestroyExceptionInfo( &exceptionInfo );
595 }
596
597 // Colorize
598 void Magick::Image::colorize ( const unsigned int alphaRed_,
599                                const unsigned int alphaGreen_,
600                                const unsigned int alphaBlue_,
601                                const Color &penColor_ )
602 {
603   if ( !penColor_.isValid() )
604   {
605     throwExceptionExplicit( OptionError,
606                             "Pen color argument is invalid");
607   }
608
609   char blend[MaxTextExtent];
610   FormatLocaleString(blend,MaxTextExtent,"%u/%u/%u",alphaRed_,alphaGreen_,alphaBlue_);
611
612   ExceptionInfo exceptionInfo;
613   GetExceptionInfo( &exceptionInfo );
614   PixelInfo target;
615   GetPixelInfo(image(),&target);
616   PixelInfo pixel=static_cast<PixelInfo>(penColor_);
617   target.red=pixel.red;
618   target.green=pixel.green;
619   target.blue=pixel.blue;
620   target.alpha=pixel.alpha;
621   MagickCore::Image* newImage =
622     ColorizeImage ( image(), blend, &target, &exceptionInfo );
623   replaceImage( newImage );
624   throwException( exceptionInfo );
625   (void) DestroyExceptionInfo( &exceptionInfo );
626 }
627 void Magick::Image::colorize ( const unsigned int alpha_,
628                                const Color &penColor_ )
629 {
630   colorize( alpha_, alpha_, alpha_, penColor_ );
631 }
632
633 // Apply a color matrix to the image channels.  The user supplied
634 // matrix may be of order 1 to 6 (1x1 through 6x6).
635 void Magick::Image::colorMatrix (const size_t order_,
636          const double *color_matrix_)
637 {
638   KernelInfo
639     *kernel_info;
640
641   ExceptionInfo exceptionInfo;
642   GetExceptionInfo( &exceptionInfo );
643   kernel_info=AcquireKernelInfo((const char *) NULL);
644   kernel_info->width=order_;
645   kernel_info->height=order_;
646   kernel_info->values=(double *) color_matrix_;
647   MagickCore::Image* newImage =
648     ColorMatrixImage( image(), kernel_info, &exceptionInfo );
649   kernel_info->values=(double *) NULL;
650   kernel_info=DestroyKernelInfo(kernel_info);
651   replaceImage( newImage );
652   throwException( exceptionInfo );
653   (void) DestroyExceptionInfo( &exceptionInfo );
654 }
655
656 // Compare current image with another image
657 // Sets meanErrorPerPixel, normalizedMaxError, and normalizedMeanError
658 // in the current image. False is returned if the images are identical.
659 bool Magick::Image::compare ( const Image &reference_ )
660 {
661   ExceptionInfo exceptionInfo;
662   GetExceptionInfo( &exceptionInfo );
663   modifyImage();
664   Image ref = reference_;
665   ref.modifyImage();
666   bool status =
667     static_cast<bool>(IsImagesEqual(image(), ref.image(), &exceptionInfo));
668   throwException( exceptionInfo );
669   (void) DestroyExceptionInfo( &exceptionInfo );
670   return status;
671 }
672
673 // Composite two images
674 void Magick::Image::composite ( const Image &compositeImage_,
675                                 const ssize_t xOffset_,
676                                 const ssize_t yOffset_,
677                                 const CompositeOperator compose_ )
678 {
679   // Image supplied as compositeImage is composited with current image and
680   // results in updating current image.
681   modifyImage();
682
683   ExceptionInfo exceptionInfo;
684   GetExceptionInfo( &exceptionInfo );
685   CompositeImage( image(),
686                   compositeImage_.constImage(),
687                   compose_, MagickFalse,
688                   xOffset_,
689                   yOffset_, &exceptionInfo );
690   throwException( exceptionInfo );
691   (void) DestroyExceptionInfo( &exceptionInfo );
692 }
693 void Magick::Image::composite ( const Image &compositeImage_,
694                                 const Geometry &offset_,
695                                 const CompositeOperator compose_ )
696 {
697   modifyImage();
698
699   ssize_t x = offset_.xOff();
700   ssize_t y = offset_.yOff();
701   size_t width = columns();
702   size_t height = rows();
703
704   ParseMetaGeometry (static_cast<std::string>(offset_).c_str(),
705                       &x, &y,
706                       &width, &height );
707
708   ExceptionInfo exceptionInfo;
709   GetExceptionInfo( &exceptionInfo );
710   CompositeImage( image(),
711                   compositeImage_.constImage(),
712                   compose_, MagickFalse,
713                   x, y, &exceptionInfo );
714   throwException( exceptionInfo );
715   (void) DestroyExceptionInfo( &exceptionInfo );
716 }
717 void Magick::Image::composite ( const Image &compositeImage_,
718                                 const GravityType gravity_,
719                                 const CompositeOperator compose_ )
720 {
721   modifyImage();
722
723   RectangleInfo geometry;
724
725   SetGeometry(compositeImage_.constImage(), &geometry);
726   GravityAdjustGeometry(columns(), rows(), gravity_, &geometry);
727
728   ExceptionInfo exceptionInfo;
729   GetExceptionInfo( &exceptionInfo );
730   CompositeImage( image(),
731                   compositeImage_.constImage(),
732                   compose_, MagickFalse,
733                   geometry.x, geometry.y, &exceptionInfo );
734   throwException( exceptionInfo );
735   (void) DestroyExceptionInfo( &exceptionInfo );
736 }
737
738 // Contrast image
739 void Magick::Image::contrast ( const size_t sharpen_ )
740 {
741   modifyImage();
742   ExceptionInfo exceptionInfo;
743   GetExceptionInfo( &exceptionInfo );
744   ContrastImage ( image(), (MagickBooleanType) sharpen_, &exceptionInfo );
745   throwException( exceptionInfo );
746   (void) DestroyExceptionInfo( &exceptionInfo );
747 }
748
749 // Convolve image.  Applies a general image convolution kernel to the image.
750 //  order_ represents the number of columns and rows in the filter kernel.
751 //  kernel_ is an array of doubles representing the convolution kernel.
752 void Magick::Image::convolve ( const size_t order_,
753                                const double *kernel_ )
754 {
755   KernelInfo
756     *kernel_info;
757
758   ExceptionInfo exceptionInfo;
759   GetExceptionInfo( &exceptionInfo );
760   kernel_info=AcquireKernelInfo((const char *) NULL);
761   kernel_info->width=order_;
762   kernel_info->height=order_;
763   kernel_info->values=(double *) kernel_;
764   MagickCore::Image* newImage =
765     ConvolveImage ( image(), kernel_info, &exceptionInfo );
766   kernel_info->values=(double *) NULL;
767   kernel_info=DestroyKernelInfo(kernel_info);
768   replaceImage( newImage );
769   throwException( exceptionInfo );
770   (void) DestroyExceptionInfo( &exceptionInfo );
771 }
772
773 // Crop image
774 void Magick::Image::crop ( const Geometry &geometry_ )
775 {
776   RectangleInfo cropInfo = geometry_;
777   ExceptionInfo exceptionInfo;
778   GetExceptionInfo( &exceptionInfo );
779   MagickCore::Image* newImage =
780     CropImage( image(),
781                &cropInfo,
782                &exceptionInfo);
783   replaceImage( newImage );
784   throwException( exceptionInfo );
785   (void) DestroyExceptionInfo( &exceptionInfo );
786 }
787
788 // Cycle Color Map
789 void Magick::Image::cycleColormap ( const ssize_t amount_ )
790 {
791   ExceptionInfo exceptionInfo;
792   GetExceptionInfo( &exceptionInfo );
793   modifyImage();
794   CycleColormapImage( image(), amount_, &exceptionInfo );
795   throwException( exceptionInfo );
796   (void) DestroyExceptionInfo( &exceptionInfo );
797 }
798
799 // Despeckle
800 void Magick::Image::despeckle ( void )
801 {
802   ExceptionInfo exceptionInfo;
803   GetExceptionInfo( &exceptionInfo );
804   MagickCore::Image* newImage =
805     DespeckleImage( image(), &exceptionInfo );
806   replaceImage( newImage );
807   throwException( exceptionInfo );
808   (void) DestroyExceptionInfo( &exceptionInfo );
809 }
810
811 // Display image
812 void Magick::Image::display( void )
813 {
814   ExceptionInfo exceptionInfo;
815   GetExceptionInfo( &exceptionInfo );
816   DisplayImages( imageInfo(), image(), &exceptionInfo );
817   throwException( exceptionInfo );
818   (void) DestroyExceptionInfo( &exceptionInfo );
819 }
820
821 // Distort image.  distorts an image using various distortion methods, by
822 // mapping color lookups of the source image to a new destination image
823 // usally of the same size as the source image, unless 'bestfit' is set to
824 // true.
825 void Magick::Image::distort ( const DistortImageMethod method_,
826                               const size_t number_arguments_,
827                               const double *arguments_,
828                               const bool bestfit_ )
829 {
830   ExceptionInfo exceptionInfo;
831   GetExceptionInfo( &exceptionInfo );
832   MagickCore::Image* newImage = DistortImage ( image(), method_,
833     number_arguments_, arguments_, bestfit_ == true ? MagickTrue : MagickFalse,
834     &exceptionInfo );
835   replaceImage( newImage );
836   throwException( exceptionInfo );
837   (void) DestroyExceptionInfo( &exceptionInfo );
838 }
839
840 // Draw on image using single drawable
841 void Magick::Image::draw ( const Magick::Drawable &drawable_ )
842 {
843   modifyImage();
844
845   DrawingWand *wand = DrawAllocateWand( options()->drawInfo(), image());
846
847   if(wand)
848     {
849       drawable_.operator()(wand);
850
851       DrawRender(wand);
852
853       wand=DestroyDrawingWand(wand);
854     }
855
856   throwImageException();
857 }
858
859 // Draw on image using a drawable list
860 void Magick::Image::draw ( const std::list<Magick::Drawable> &drawable_ )
861 {
862   modifyImage();
863
864   DrawingWand *wand = DrawAllocateWand( options()->drawInfo(), image());
865
866   if(wand)
867     {
868       for( std::list<Magick::Drawable>::const_iterator p = drawable_.begin();
869            p != drawable_.end(); p++ )
870         {
871           p->operator()(wand);
872         }
873
874       DrawRender(wand);
875
876       wand=DestroyDrawingWand(wand);
877     }
878
879   throwImageException();
880 }
881
882 // Hilight edges in image
883 void Magick::Image::edge ( const double radius_, const double sigma_ )
884 {
885   ExceptionInfo exceptionInfo;
886   GetExceptionInfo( &exceptionInfo );
887   MagickCore::Image* newImage =
888     EdgeImage( image(), radius_, sigma_, &exceptionInfo );
889   replaceImage( newImage );
890   throwException( exceptionInfo );
891   (void) DestroyExceptionInfo( &exceptionInfo );
892 }
893
894 // Emboss image (hilight edges)
895 void Magick::Image::emboss ( const double radius_, const double sigma_ )
896 {
897   ExceptionInfo exceptionInfo;
898   GetExceptionInfo( &exceptionInfo );
899   MagickCore::Image* newImage =
900     EmbossImage( image(), radius_, sigma_, &exceptionInfo );
901   replaceImage( newImage );
902   throwException( exceptionInfo );
903   (void) DestroyExceptionInfo( &exceptionInfo );
904 }
905
906 // Enhance image (minimize noise)
907 void Magick::Image::enhance ( void )
908 {
909   ExceptionInfo exceptionInfo;
910   GetExceptionInfo( &exceptionInfo );
911   MagickCore::Image* newImage =
912     EnhanceImage( image(), &exceptionInfo );
913   replaceImage( newImage );
914   throwException( exceptionInfo );
915   (void) DestroyExceptionInfo( &exceptionInfo );
916 }
917
918 // Equalize image (histogram equalization)
919 void Magick::Image::equalize ( void )
920 {
921   ExceptionInfo exceptionInfo;
922   GetExceptionInfo( &exceptionInfo );
923   modifyImage();
924   EqualizeImage( image(), &exceptionInfo );
925   throwException( exceptionInfo );
926   (void) DestroyExceptionInfo( &exceptionInfo );
927 }
928
929 // Erase image to current "background color"
930 void Magick::Image::erase ( void )
931 {
932   modifyImage();
933   ExceptionInfo exceptionInfo;
934   GetExceptionInfo( &exceptionInfo );
935   SetImageBackgroundColor( image(), &exceptionInfo );
936   throwException( exceptionInfo );
937   (void) DestroyExceptionInfo( &exceptionInfo );
938 }
939
940 // Extends image as defined by the geometry.
941 //
942 void Magick::Image::extent ( const Geometry &geometry_ )
943 {
944   RectangleInfo extentInfo = geometry_;
945   modifyImage();
946   ExceptionInfo exceptionInfo;
947   GetExceptionInfo( &exceptionInfo );
948   MagickCore::Image* newImage =
949     ExtentImage ( image(), &extentInfo, &exceptionInfo );
950   replaceImage( newImage );
951   throwException( exceptionInfo );
952   (void) DestroyExceptionInfo( &exceptionInfo );
953 }
954 void Magick::Image::extent ( const Geometry &geometry_, const Color &backgroundColor_ )
955 {
956   backgroundColor ( backgroundColor_ );
957   extent ( geometry_ );
958 }
959 void Magick::Image::extent ( const Geometry &geometry_, const GravityType gravity_ )
960 {
961   RectangleInfo geometry;
962
963   SetGeometry(image(), &geometry);
964   GravityAdjustGeometry(image()->columns, image()->rows, gravity_, &geometry);
965   extent ( geometry_ );
966 }
967 void Magick::Image::extent ( const Geometry &geometry_, const Color &backgroundColor_, const GravityType gravity_ )
968 {
969   image()->gravity  = gravity_;
970   backgroundColor ( backgroundColor_ );
971   extent ( geometry_ );
972 }
973
974 // Flip image (reflect each scanline in the vertical direction)
975 void Magick::Image::flip ( void )
976 {
977   ExceptionInfo exceptionInfo;
978   GetExceptionInfo( &exceptionInfo );
979   MagickCore::Image* newImage =
980     FlipImage( image(), &exceptionInfo );
981   replaceImage( newImage );
982   throwException( exceptionInfo );
983   (void) DestroyExceptionInfo( &exceptionInfo );
984 }
985
986 // Flood-fill color across pixels that match the color of the
987 // target pixel and are neighbors of the target pixel.
988 // Uses current fuzz setting when determining color match.
989 void Magick::Image::floodFillColor( const ssize_t x_,
990                                     const ssize_t y_,
991                                     const Magick::Color &fillColor_ )
992 {
993   floodFillTexture( x_, y_, Image( Geometry( 1, 1), fillColor_ ) );
994 }
995 void Magick::Image::floodFillColor( const Geometry &point_,
996                                     const Magick::Color &fillColor_ )
997 {
998   floodFillTexture( point_, Image( Geometry( 1, 1), fillColor_) );
999 }
1000
1001 // Flood-fill color across pixels starting at target-pixel and
1002 // stopping at pixels matching specified border color.
1003 // Uses current fuzz setting when determining color match.
1004 void Magick::Image::floodFillColor( const ssize_t x_,
1005                                     const ssize_t y_,
1006                                     const Magick::Color &fillColor_,
1007                                     const Magick::Color &borderColor_ )
1008 {
1009   floodFillTexture( x_, y_, Image( Geometry( 1, 1), fillColor_),
1010                     borderColor_ );
1011 }
1012 void Magick::Image::floodFillColor( const Geometry &point_,
1013                                     const Magick::Color &fillColor_,
1014                                     const Magick::Color &borderColor_ )
1015 {
1016   floodFillTexture( point_, Image( Geometry( 1, 1), fillColor_),
1017                     borderColor_ );
1018 }
1019
1020 // Floodfill pixels matching color (within fuzz factor) of target
1021 // pixel(x,y) with replacement alpha value using method.
1022 void Magick::Image::floodFillAlpha( const ssize_t x_,
1023                                       const ssize_t y_,
1024                                       const unsigned int alpha_,
1025                                       const PaintMethod method_ )
1026 {
1027   modifyImage();
1028   PixelInfo target;
1029   GetPixelInfo(image(),&target);
1030   PixelInfo pixel=static_cast<PixelInfo>(pixelColor(x_,y_));
1031   target.red=pixel.red;
1032   target.green=pixel.green;
1033   target.blue=pixel.blue;
1034   target.alpha=alpha_;
1035   ExceptionInfo exceptionInfo;
1036   GetExceptionInfo( &exceptionInfo );
1037   FloodfillPaintImage ( image(),
1038                         options()->drawInfo(), // const DrawInfo *draw_info
1039                         &target,
1040                                         static_cast<ssize_t>(x_), static_cast<ssize_t>(y_),
1041                         method_  == FloodfillMethod ? MagickFalse : MagickTrue,
1042     &exceptionInfo);
1043   throwException( exceptionInfo );
1044   (void) DestroyExceptionInfo( &exceptionInfo );
1045 }
1046
1047 // Flood-fill texture across pixels that match the color of the
1048 // target pixel and are neighbors of the target pixel.
1049 // Uses current fuzz setting when determining color match.
1050 void Magick::Image::floodFillTexture( const ssize_t x_,
1051                                       const ssize_t y_,
1052                                       const Magick::Image &texture_ )
1053 {
1054   modifyImage();
1055
1056   // Set drawing pattern
1057   options()->fillPattern(texture_.constImage());
1058
1059   // Get pixel view
1060   Pixels pixels(*this);
1061   // Fill image
1062   Quantum *p = pixels.get(x_, y_, 1, 1 );
1063   PixelInfo target;
1064   GetPixelInfo(constImage(),&target);
1065   target.red=GetPixelRed(constImage(),p);
1066   target.green=GetPixelGreen(constImage(),p);
1067   target.blue=GetPixelBlue(constImage(),p);
1068   ExceptionInfo exceptionInfo;
1069   GetExceptionInfo( &exceptionInfo );
1070   if (p)
1071     FloodfillPaintImage ( image(), // Image *image
1072                           options()->drawInfo(), // const DrawInfo *draw_info
1073                           &target, // const MagickPacket target
1074                           static_cast<ssize_t>(x_), // const ssize_t x_offset
1075                           static_cast<ssize_t>(y_), // const ssize_t y_offset
1076                           MagickFalse, // const PaintMethod method
1077       &exceptionInfo );
1078   throwException( exceptionInfo );
1079   (void) DestroyExceptionInfo( &exceptionInfo );
1080
1081 }
1082 void Magick::Image::floodFillTexture( const Magick::Geometry &point_,
1083                                       const Magick::Image &texture_ )
1084 {
1085   floodFillTexture( point_.xOff(), point_.yOff(), texture_ );
1086 }
1087
1088 // Flood-fill texture across pixels starting at target-pixel and
1089 // stopping at pixels matching specified border color.
1090 // Uses current fuzz setting when determining color match.
1091 void Magick::Image::floodFillTexture( const ssize_t x_,
1092                                       const ssize_t y_,
1093                                       const Magick::Image &texture_,
1094                                       const Magick::Color &borderColor_ )
1095 {
1096   modifyImage();
1097
1098   // Set drawing fill pattern
1099   options()->fillPattern(texture_.constImage());
1100
1101   PixelInfo target;
1102   GetPixelInfo(constImage(),&target);
1103   target.red=static_cast<PixelInfo>(borderColor_).red;
1104   target.green=static_cast<PixelInfo>(borderColor_).green;
1105   target.blue=static_cast<PixelInfo>(borderColor_).blue;
1106   ExceptionInfo exceptionInfo;
1107   GetExceptionInfo( &exceptionInfo );
1108   FloodfillPaintImage ( image(),
1109                         options()->drawInfo(),
1110                         &target,
1111                         static_cast<ssize_t>(x_),
1112                         static_cast<ssize_t>(y_),
1113                         MagickTrue, &exceptionInfo);
1114
1115   throwException( exceptionInfo );
1116   (void) DestroyExceptionInfo( &exceptionInfo );
1117 }
1118 void  Magick::Image::floodFillTexture( const Magick::Geometry &point_,
1119                                        const Magick::Image &texture_,
1120                                        const Magick::Color &borderColor_ )
1121 {
1122   floodFillTexture( point_.xOff(), point_.yOff(), texture_, borderColor_ );
1123 }
1124
1125 // Flop image (reflect each scanline in the horizontal direction)
1126 void Magick::Image::flop ( void )
1127 {
1128   ExceptionInfo exceptionInfo;
1129   GetExceptionInfo( &exceptionInfo );
1130   MagickCore::Image* newImage =
1131     FlopImage( image(), &exceptionInfo );
1132   replaceImage( newImage );
1133   throwException( exceptionInfo );
1134   (void) DestroyExceptionInfo( &exceptionInfo );
1135 }
1136
1137 // Frame image
1138 void Magick::Image::frame ( const Geometry &geometry_ )
1139 {
1140   FrameInfo info;
1141
1142   info.x           = static_cast<ssize_t>(geometry_.width());
1143   info.y           = static_cast<ssize_t>(geometry_.height());
1144   info.width       = columns() + ( static_cast<size_t>(info.x) << 1 );
1145   info.height      = rows() + ( static_cast<size_t>(info.y) << 1 );
1146   info.outer_bevel = geometry_.xOff();
1147   info.inner_bevel = geometry_.yOff();
1148
1149   ExceptionInfo exceptionInfo;
1150   GetExceptionInfo( &exceptionInfo );
1151   MagickCore::Image* newImage =
1152     FrameImage( image(), &info, image()->compose, &exceptionInfo );
1153   replaceImage( newImage );
1154   throwException( exceptionInfo );
1155   (void) DestroyExceptionInfo( &exceptionInfo );
1156 }
1157 void Magick::Image::frame ( const size_t width_,
1158                             const size_t height_,
1159                             const ssize_t outerBevel_, const ssize_t innerBevel_ )
1160 {
1161   FrameInfo info;
1162   info.x           = static_cast<ssize_t>(width_);
1163   info.y           = static_cast<ssize_t>(height_);
1164   info.width       = columns() + ( static_cast<size_t>(info.x) << 1 );
1165   info.height      = rows() + ( static_cast<size_t>(info.y) << 1 );
1166   info.outer_bevel = static_cast<ssize_t>(outerBevel_);
1167   info.inner_bevel = static_cast<ssize_t>(innerBevel_);
1168
1169   ExceptionInfo exceptionInfo;
1170   GetExceptionInfo( &exceptionInfo );
1171   MagickCore::Image* newImage =
1172     FrameImage( image(), &info, image()->compose, &exceptionInfo );
1173   replaceImage( newImage );
1174   throwException( exceptionInfo );
1175   (void) DestroyExceptionInfo( &exceptionInfo );
1176 }
1177
1178 // Fx image.  Applies a mathematical expression to the image.
1179 void Magick::Image::fx ( const std::string expression )
1180 {
1181   ExceptionInfo exceptionInfo;
1182   GetExceptionInfo( &exceptionInfo );
1183   MagickCore::Image* newImage =
1184     FxImage ( image(), expression.c_str(), &exceptionInfo );
1185   replaceImage( newImage );
1186   throwException( exceptionInfo );
1187   (void) DestroyExceptionInfo( &exceptionInfo );
1188 }
1189 void Magick::Image::fx ( const std::string expression,
1190                          const Magick::ChannelType channel )
1191 {
1192   ExceptionInfo exceptionInfo;
1193   GetExceptionInfo( &exceptionInfo );
1194   ChannelType channel_mask = SetPixelChannelMask( image(), channel );
1195   MagickCore::Image* newImage =
1196     FxImage ( image(), expression.c_str(), &exceptionInfo );
1197   (void) SetPixelChannelMapMask( image(), channel_mask );
1198   replaceImage( newImage );
1199   throwException( exceptionInfo );
1200   (void) DestroyExceptionInfo( &exceptionInfo );
1201 }
1202
1203 // Gamma correct image
1204 void Magick::Image::gamma ( const double gamma_ )
1205 {
1206   ExceptionInfo exceptionInfo;
1207   GetExceptionInfo( &exceptionInfo );
1208   modifyImage();
1209   GammaImage ( image(), gamma_, &exceptionInfo );
1210   throwException( exceptionInfo );
1211   (void) DestroyExceptionInfo( &exceptionInfo );
1212 }
1213
1214 void Magick::Image::gamma ( const double gammaRed_,
1215                             const double gammaGreen_,
1216                             const double gammaBlue_ )
1217 {
1218   char gamma[MaxTextExtent + 1];
1219   FormatLocaleString( gamma, MaxTextExtent, "%3.6f/%3.6f/%3.6f/",
1220                 gammaRed_, gammaGreen_, gammaBlue_);
1221
1222   ExceptionInfo exceptionInfo;
1223   GetExceptionInfo( &exceptionInfo );
1224   modifyImage();
1225   GammaImage ( image(), atof(gamma), &exceptionInfo );
1226   throwException( exceptionInfo );
1227   (void) DestroyExceptionInfo( &exceptionInfo );
1228 }
1229
1230 // Gaussian blur image
1231 // The number of neighbor pixels to be included in the convolution
1232 // mask is specified by 'width_'. The standard deviation of the
1233 // gaussian bell curve is specified by 'sigma_'.
1234 void Magick::Image::gaussianBlur ( const double width_, const double sigma_ )
1235 {
1236   ExceptionInfo exceptionInfo;
1237   GetExceptionInfo( &exceptionInfo );
1238   MagickCore::Image* newImage =
1239     GaussianBlurImage( image(), width_, sigma_, &exceptionInfo );
1240   replaceImage( newImage );
1241   throwException( exceptionInfo );
1242   (void) DestroyExceptionInfo( &exceptionInfo );
1243 }
1244
1245 void Magick::Image::gaussianBlurChannel ( const ChannelType channel_,
1246                                           const double width_,
1247                                           const double sigma_ )
1248 {
1249   ExceptionInfo exceptionInfo;
1250   GetExceptionInfo( &exceptionInfo );
1251   ChannelType channel_mask = SetPixelChannelMask( image(), channel_ );
1252   MagickCore::Image* newImage =
1253     GaussianBlurImage( image(), width_, sigma_, &exceptionInfo );
1254   (void) SetPixelChannelMapMask( image(), channel_mask );
1255   replaceImage( newImage );
1256   throwException( exceptionInfo );
1257   (void) DestroyExceptionInfo( &exceptionInfo );
1258 }
1259
1260 // Apply a color lookup table (Hald CLUT) to the image.
1261 void  Magick::Image::haldClut ( const Image &clutImage_ )
1262 {
1263   ExceptionInfo exceptionInfo;
1264   GetExceptionInfo( &exceptionInfo );
1265   modifyImage();
1266   (void) HaldClutImage( image(), clutImage_.constImage(), &exceptionInfo );
1267   throwException( exceptionInfo );
1268   (void) DestroyExceptionInfo( &exceptionInfo );
1269 }
1270
1271 // Implode image
1272 void Magick::Image::implode ( const double factor_ )
1273 {
1274   ExceptionInfo exceptionInfo;
1275   GetExceptionInfo( &exceptionInfo );
1276   MagickCore::Image* newImage =
1277     ImplodeImage( image(), factor_, image()->interpolate, &exceptionInfo );
1278   replaceImage( newImage );
1279   throwException( exceptionInfo );
1280   (void) DestroyExceptionInfo( &exceptionInfo );
1281 }
1282
1283 // implements the inverse discrete Fourier transform (IFT) of the image either
1284 // as a magnitude / phase or real / imaginary image pair.
1285 void Magick::Image::inverseFourierTransform ( const Image &phase_ )
1286 {
1287   ExceptionInfo exceptionInfo;
1288   GetExceptionInfo( &exceptionInfo );
1289   MagickCore::Image* newImage = InverseFourierTransformImage( image(),
1290                  phase_.constImage(), MagickTrue, &exceptionInfo);
1291   replaceImage( newImage );
1292   throwException( exceptionInfo );
1293   (void) DestroyExceptionInfo( &exceptionInfo );
1294 }
1295 void Magick::Image::inverseFourierTransform ( const Image &phase_,
1296    const bool magnitude_ )
1297 {
1298   ExceptionInfo exceptionInfo;
1299   GetExceptionInfo( &exceptionInfo );
1300   MagickCore::Image* newImage = InverseFourierTransformImage( image(),
1301                  phase_.constImage(), magnitude_ == true ? MagickTrue : MagickFalse,
1302      &exceptionInfo);
1303   replaceImage( newImage );
1304   throwException( exceptionInfo );
1305   (void) DestroyExceptionInfo( &exceptionInfo );
1306 }
1307
1308 // Level image. Adjust the levels of the image by scaling the colors
1309 // falling between specified white and black points to the full
1310 // available quantum range. The parameters provided represent the
1311 // black, mid (gamma), and white points.  The black point specifies
1312 // the darkest color in the image. Colors darker than the black point
1313 // are set to zero. Mid point (gamma) specifies a gamma correction to
1314 // apply to the image. White point specifies the lightest color in the
1315 // image.  Colors brighter than the white point are set to the maximum
1316 // quantum value. The black and white point have the valid range 0 to
1317 // QuantumRange while gamma has a useful range of 0 to ten.
1318 void Magick::Image::level ( const double black_point,
1319                             const double white_point,
1320                             const double gamma )
1321 {
1322   ExceptionInfo exceptionInfo;
1323   GetExceptionInfo( &exceptionInfo );
1324   modifyImage();
1325   (void) LevelImage( image(), black_point, white_point, gamma, &exceptionInfo );
1326   throwException( exceptionInfo );
1327   (void) DestroyExceptionInfo( &exceptionInfo );
1328 }
1329
1330 // Magnify image by integral size
1331 void Magick::Image::magnify ( void )
1332 {
1333   ExceptionInfo exceptionInfo;
1334   GetExceptionInfo( &exceptionInfo );
1335   MagickCore::Image* newImage =
1336     MagnifyImage( image(), &exceptionInfo );
1337   replaceImage( newImage );
1338   throwException( exceptionInfo );
1339   (void) DestroyExceptionInfo( &exceptionInfo );
1340 }
1341
1342 // Remap image colors with closest color from reference image
1343 void Magick::Image::map ( const Image &mapImage_ , const bool dither_ )
1344 {
1345   ExceptionInfo exceptionInfo;
1346   GetExceptionInfo( &exceptionInfo );
1347   modifyImage();
1348   options()->quantizeDither( dither_ );
1349   RemapImage ( options()->quantizeInfo(), image(),
1350              mapImage_.constImage(), &exceptionInfo);
1351   throwException( exceptionInfo );
1352   (void) DestroyExceptionInfo( &exceptionInfo );
1353 }
1354 // Floodfill designated area with replacement alpha value
1355 void Magick::Image::matteFloodfill ( const Color &target_ ,
1356                                      const unsigned int alpha_,
1357                                      const ssize_t x_, const ssize_t y_,
1358                                      const Magick::PaintMethod method_ )
1359 {
1360   modifyImage();
1361   PixelInfo target;
1362   GetPixelInfo(constImage(),&target);
1363   target.red=static_cast<PixelInfo>(target_).red;
1364   target.green=static_cast<PixelInfo>(target_).green;
1365   target.blue=static_cast<PixelInfo>(target_).blue;
1366   target.alpha=alpha_;
1367   ChannelType channel_mask = SetPixelChannelMask( image(), AlphaChannel );
1368   ExceptionInfo exceptionInfo;
1369   GetExceptionInfo( &exceptionInfo );
1370   FloodfillPaintImage ( image(), options()->drawInfo(), &target, x_, y_,
1371     method_ == FloodfillMethod ? MagickFalse : MagickTrue, &exceptionInfo);
1372   (void) SetPixelChannelMapMask( image(), channel_mask );
1373   throwException( exceptionInfo );
1374   (void) DestroyExceptionInfo( &exceptionInfo );
1375 }
1376
1377 // Filter image by replacing each pixel component with the median
1378 // color in a circular neighborhood
1379 void Magick::Image::medianFilter ( const double radius_ )
1380 {
1381   ExceptionInfo exceptionInfo;
1382   GetExceptionInfo( &exceptionInfo );
1383   MagickCore::Image* newImage =
1384     StatisticImage ( image(), MedianStatistic, (size_t) radius_, (size_t)
1385     radius_,&exceptionInfo );
1386   replaceImage( newImage );
1387   throwException( exceptionInfo );
1388   (void) DestroyExceptionInfo( &exceptionInfo );
1389 }
1390
1391 // Reduce image by integral size
1392 void Magick::Image::minify ( void )
1393 {
1394   ExceptionInfo exceptionInfo;
1395   GetExceptionInfo( &exceptionInfo );
1396   MagickCore::Image* newImage =
1397     MinifyImage( image(), &exceptionInfo );
1398   replaceImage( newImage );
1399   throwException( exceptionInfo );
1400   (void) DestroyExceptionInfo( &exceptionInfo );
1401 }
1402
1403 // Modulate percent hue, saturation, and brightness of an image
1404 void Magick::Image::modulate ( const double brightness_,
1405                                const double saturation_,
1406                                const double hue_ )
1407 {
1408   char modulate[MaxTextExtent + 1];
1409   FormatLocaleString( modulate, MaxTextExtent, "%3.6f,%3.6f,%3.6f",
1410                 brightness_, saturation_, hue_);
1411
1412   ExceptionInfo exceptionInfo;
1413   GetExceptionInfo( &exceptionInfo );
1414   modifyImage();
1415   ModulateImage( image(), modulate, &exceptionInfo );
1416   throwException( exceptionInfo );
1417   (void) DestroyExceptionInfo( &exceptionInfo );
1418 }
1419
1420 // Motion blur image with specified blur factor
1421 // The radius_ parameter specifies the radius of the Gaussian, in
1422 // pixels, not counting the center pixel.  The sigma_ parameter
1423 // specifies the standard deviation of the Laplacian, in pixels.
1424 // The angle_ parameter specifies the angle the object appears
1425 // to be comming from (zero degrees is from the right).
1426 void            Magick::Image::motionBlur ( const double radius_,
1427                                             const double sigma_,
1428                                             const double angle_ )
1429 {
1430   ExceptionInfo exceptionInfo;
1431   GetExceptionInfo( &exceptionInfo );
1432   MagickCore::Image* newImage =
1433     MotionBlurImage( image(), radius_, sigma_, angle_, &exceptionInfo);
1434   replaceImage( newImage );
1435   throwException( exceptionInfo );
1436   (void) DestroyExceptionInfo( &exceptionInfo );
1437 }
1438     
1439 // Negate image.  Set grayscale_ to true to effect grayscale values
1440 // only
1441 void Magick::Image::negate ( const bool grayscale_ )
1442 {
1443   ExceptionInfo exceptionInfo;
1444   GetExceptionInfo( &exceptionInfo );
1445   modifyImage();
1446   NegateImage ( image(), grayscale_ == true ? MagickTrue : MagickFalse, 
1447     &exceptionInfo );
1448   throwException( exceptionInfo );
1449   (void) DestroyExceptionInfo( &exceptionInfo );
1450 }
1451
1452 // Normalize image
1453 void Magick::Image::normalize ( void )
1454 {
1455   modifyImage();
1456   ExceptionInfo exceptionInfo;
1457   GetExceptionInfo( &exceptionInfo );
1458   NormalizeImage ( image(), &exceptionInfo );
1459   throwException( exceptionInfo );
1460   (void) DestroyExceptionInfo( &exceptionInfo );
1461 }
1462
1463 // Oilpaint image
1464 void Magick::Image::oilPaint ( const double radius_, const double sigma_ )
1465 {
1466   ExceptionInfo exceptionInfo;
1467   GetExceptionInfo( &exceptionInfo );
1468   MagickCore::Image* newImage =
1469     OilPaintImage( image(), radius_, sigma_, &exceptionInfo );
1470   replaceImage( newImage );
1471   throwException( exceptionInfo );
1472   (void) DestroyExceptionInfo( &exceptionInfo );
1473 }
1474
1475 // Set or attenuate the alpha channel. If the image pixels are
1476 // opaque then they are set to the specified alpha value, otherwise
1477 // they are blended with the supplied alpha value.  The value of
1478 // alpha_ ranges from 0 (completely opaque) to QuantumRange. The defines
1479 // OpaqueAlpha and TransparentAlpha are available to specify
1480 // completely opaque or completely transparent, respectively.
1481 void Magick::Image::alpha ( const unsigned int alpha_ )
1482 {
1483   modifyImage();
1484   ExceptionInfo exceptionInfo;
1485   GetExceptionInfo( &exceptionInfo );
1486   SetImageAlpha( image(), alpha_, &exceptionInfo );
1487   throwException( exceptionInfo );
1488   (void) DestroyExceptionInfo( &exceptionInfo );
1489 }
1490
1491 // Change the color of an opaque pixel to the pen color.
1492 void Magick::Image::opaque ( const Color &opaqueColor_,
1493                              const Color &penColor_ )
1494 {
1495   if ( !opaqueColor_.isValid() )
1496   {
1497     throwExceptionExplicit( OptionError,
1498                             "Opaque color argument is invalid" );
1499   }
1500   if ( !penColor_.isValid() )
1501   {
1502     throwExceptionExplicit( OptionError,
1503                             "Pen color argument is invalid" );
1504   }
1505
1506   modifyImage();
1507   std::string opaqueColor = opaqueColor_;
1508   std::string penColor = penColor_;
1509
1510   PixelInfo opaque;
1511   PixelInfo pen;
1512   ExceptionInfo exceptionInfo;
1513   GetExceptionInfo( &exceptionInfo );
1514   (void) QueryColorCompliance(std::string(opaqueColor_).c_str(),
1515     AllCompliance, &opaque, &exceptionInfo);
1516   (void) QueryColorCompliance(std::string(penColor_).c_str(),
1517     AllCompliance, &pen, &exceptionInfo);
1518   OpaquePaintImage ( image(), &opaque, &pen, MagickFalse, &exceptionInfo );
1519   throwException( exceptionInfo );
1520   (void) DestroyExceptionInfo( &exceptionInfo );
1521 }
1522
1523 // Ping is similar to read except only enough of the image is read to
1524 // determine the image columns, rows, and filesize.  Access the
1525 // columns(), rows(), and fileSize() attributes after invoking ping.
1526 // The image data is not valid after calling ping.
1527 void Magick::Image::ping ( const std::string &imageSpec_ )
1528 {
1529   options()->fileName( imageSpec_ );
1530   ExceptionInfo exceptionInfo;
1531   GetExceptionInfo( &exceptionInfo );
1532   MagickCore::Image* image =
1533     PingImage( imageInfo(), &exceptionInfo );
1534   replaceImage( image );
1535   throwException( exceptionInfo );
1536   (void) DestroyExceptionInfo( &exceptionInfo );
1537 }
1538
1539 // Ping is similar to read except only enough of the image is read
1540 // to determine the image columns, rows, and filesize.  Access the
1541 // columns(), rows(), and fileSize() attributes after invoking
1542 // ping.  The image data is not valid after calling ping.
1543 void Magick::Image::ping ( const Blob& blob_ )
1544 {
1545   ExceptionInfo exceptionInfo;
1546   GetExceptionInfo( &exceptionInfo );
1547   MagickCore::Image* image =
1548     PingBlob( imageInfo(), blob_.data(), blob_.length(), &exceptionInfo );
1549   replaceImage( image );
1550   throwException( exceptionInfo );
1551   (void) DestroyExceptionInfo( &exceptionInfo );
1552 }
1553
1554 // Execute a named process module using an argc/argv syntax similar to
1555 // that accepted by a C 'main' routine. An exception is thrown if the
1556 // requested process module doesn't exist, fails to load, or fails during
1557 // execution.
1558 void Magick::Image::process( std::string name_, const ssize_t argc, const char **argv )
1559 {
1560   modifyImage();
1561
1562   ExceptionInfo exceptionInfo;
1563   GetExceptionInfo( &exceptionInfo );
1564   size_t status = 
1565     InvokeDynamicImageFilter( name_.c_str(), &image(), argc, argv,
1566       &exceptionInfo );
1567   (void) status;
1568   throwException( exceptionInfo );
1569   (void) DestroyExceptionInfo( &exceptionInfo );
1570 }
1571
1572 // Quantize colors in image using current quantization settings
1573 // Set measureError_ to true in order to measure quantization error
1574 void Magick::Image::quantize ( const bool measureError_  )
1575 {
1576   modifyImage();
1577  
1578   if (measureError_)
1579     options()->quantizeInfo()->measure_error=MagickTrue;
1580   else
1581     options()->quantizeInfo()->measure_error=MagickFalse;
1582
1583   ExceptionInfo exceptionInfo;
1584   GetExceptionInfo( &exceptionInfo );
1585   QuantizeImage( options()->quantizeInfo(), image(), &exceptionInfo );
1586
1587   throwException( exceptionInfo );
1588   (void) DestroyExceptionInfo( &exceptionInfo );
1589 }
1590
1591 // Apply an arithmetic or bitwise operator to the image pixel quantums.
1592 void Magick::Image::quantumOperator ( const ChannelType channel_,
1593                                       const MagickEvaluateOperator operator_,
1594                                       double rvalue_)
1595 {
1596   ExceptionInfo exceptionInfo;
1597   GetExceptionInfo( &exceptionInfo );
1598   ChannelType channel_mask = SetPixelChannelMask( image(), channel_ );
1599   EvaluateImage( image(), operator_, rvalue_, &exceptionInfo);
1600   (void) SetPixelChannelMapMask( image(), channel_mask );
1601   throwException( exceptionInfo );
1602   (void) DestroyExceptionInfo( &exceptionInfo );
1603 }
1604
1605 void Magick::Image::quantumOperator ( const ssize_t x_,const ssize_t y_,
1606                                       const size_t columns_,
1607                                       const size_t rows_,
1608                                       const ChannelType channel_,
1609                                       const MagickEvaluateOperator operator_,
1610                                       const double rvalue_)
1611 {
1612   ExceptionInfo exceptionInfo;
1613   GetExceptionInfo( &exceptionInfo );
1614   RectangleInfo geometry;
1615   geometry.width = columns_;
1616   geometry.height = rows_;
1617   geometry.x = x_;
1618   geometry.y = y_;
1619   MagickCore::Image *crop_image = CropImage( image(), &geometry,
1620     &exceptionInfo );
1621   ChannelType channel_mask = SetPixelChannelMask( image(), channel_);
1622   EvaluateImage( crop_image, operator_, rvalue_, &exceptionInfo );
1623   (void) SetPixelChannelMapMask( image(), channel_mask );
1624   (void) CompositeImage( image(), crop_image, image()->matte != MagickFalse ?
1625     OverCompositeOp : CopyCompositeOp, MagickFalse, geometry.x, geometry.y,
1626     &exceptionInfo );
1627   crop_image = DestroyImageList(crop_image);
1628   throwException( exceptionInfo );
1629   (void) DestroyExceptionInfo( &exceptionInfo );
1630 }
1631
1632 // Raise image (lighten or darken the edges of an image to give a 3-D
1633 // raised or lowered effect)
1634 void Magick::Image::raise ( const Geometry &geometry_ ,
1635                             const bool raisedFlag_ )
1636 {
1637   ExceptionInfo exceptionInfo;
1638   GetExceptionInfo( &exceptionInfo );
1639   RectangleInfo raiseInfo = geometry_;
1640   modifyImage();
1641   RaiseImage ( image(), &raiseInfo, raisedFlag_ == true ? MagickTrue : MagickFalse, &exceptionInfo );
1642   throwException( exceptionInfo );
1643   (void) DestroyExceptionInfo( &exceptionInfo );
1644 }
1645
1646
1647 // Random threshold image.
1648 //
1649 // Changes the value of individual pixels based on the intensity
1650 // of each pixel compared to a random threshold.  The result is a
1651 // low-contrast, two color image.  The thresholds_ argument is a
1652 // geometry containing LOWxHIGH thresholds.  If the string
1653 // contains 2x2, 3x3, or 4x4, then an ordered dither of order 2,
1654 // 3, or 4 will be performed instead.  If a channel_ argument is
1655 // specified then only the specified channel is altered.  This is
1656 // a very fast alternative to 'quantize' based dithering.
1657 void Magick::Image::randomThreshold( const Geometry &thresholds_ )
1658 {
1659   randomThresholdChannel(thresholds_,DefaultChannels);
1660 }
1661 void Magick::Image::randomThresholdChannel( const Geometry &thresholds_,
1662                                             const ChannelType channel_ )
1663 {
1664   ExceptionInfo exceptionInfo;
1665   GetExceptionInfo( &exceptionInfo );
1666   modifyImage();
1667   ChannelType channel_mask = SetPixelChannelMask( image(), channel_);
1668   (void) RandomThresholdImage( image(),
1669                                       static_cast<std::string>(thresholds_).c_str(),
1670                                       &exceptionInfo );
1671   (void) SetPixelChannelMapMask( image(), channel_mask );
1672   throwImageException();
1673   (void) DestroyExceptionInfo( &exceptionInfo );
1674 }
1675     
1676 // Read image into current object
1677 void Magick::Image::read ( const std::string &imageSpec_ )
1678 {
1679   options()->fileName( imageSpec_ );
1680
1681   ExceptionInfo exceptionInfo;
1682   GetExceptionInfo( &exceptionInfo );
1683   MagickCore::Image* image =
1684     ReadImage( imageInfo(), &exceptionInfo );
1685
1686   // Ensure that multiple image frames were not read.
1687   if ( image && image->next )
1688     {
1689       // Destroy any extra image frames
1690       MagickCore::Image* next = image->next;
1691       image->next = 0;
1692       next->previous = 0;
1693       DestroyImageList( next );
1694  
1695     }
1696   replaceImage( image );
1697   throwException( exceptionInfo );
1698   (void) DestroyExceptionInfo( &exceptionInfo );
1699 }
1700
1701 // Read image of specified size into current object
1702 void Magick::Image::read ( const Geometry &size_,
1703                            const std::string &imageSpec_ )
1704 {
1705   size( size_ );
1706   read( imageSpec_ );
1707 }
1708
1709 // Read image from in-memory BLOB
1710 void Magick::Image::read ( const Blob &blob_ )
1711 {
1712   ExceptionInfo exceptionInfo;
1713   GetExceptionInfo( &exceptionInfo );
1714   MagickCore::Image* image =
1715     BlobToImage( imageInfo(),
1716                  static_cast<const void *>(blob_.data()),
1717                  blob_.length(), &exceptionInfo );
1718   replaceImage( image );
1719   throwException( exceptionInfo );
1720   (void) DestroyExceptionInfo( &exceptionInfo );
1721 }
1722
1723 // Read image of specified size from in-memory BLOB
1724 void  Magick::Image::read ( const Blob &blob_,
1725                             const Geometry &size_ )
1726 {
1727   // Set image size
1728   size( size_ );
1729   // Read from Blob
1730   read( blob_ );
1731 }
1732
1733 // Read image of specified size and depth from in-memory BLOB
1734 void Magick::Image::read ( const Blob &blob_,
1735                            const Geometry &size_,
1736                            const size_t depth_ )
1737 {
1738   // Set image size
1739   size( size_ );
1740   // Set image depth
1741   depth( depth_ );
1742   // Read from Blob
1743   read( blob_ );
1744 }
1745
1746 // Read image of specified size, depth, and format from in-memory BLOB
1747 void Magick::Image::read ( const Blob &blob_,
1748                            const Geometry &size_,
1749                            const size_t depth_,
1750                            const std::string &magick_ )
1751 {
1752   // Set image size
1753   size( size_ );
1754   // Set image depth
1755   depth( depth_ );
1756   // Set image magick
1757   magick( magick_ );
1758   // Set explicit image format
1759   fileName( magick_ + ':');
1760   // Read from Blob
1761   read( blob_ );
1762 }
1763
1764 // Read image of specified size, and format from in-memory BLOB
1765 void Magick::Image::read ( const Blob &blob_,
1766                            const Geometry &size_,
1767                            const std::string &magick_ )
1768 {
1769   // Set image size
1770   size( size_ );
1771   // Set image magick
1772   magick( magick_ );
1773   // Set explicit image format
1774   fileName( magick_ + ':');
1775   // Read from Blob
1776   read( blob_ );
1777 }
1778
1779 // Read image based on raw pixels in memory (ConstituteImage)
1780 void Magick::Image::read ( const size_t width_,
1781                            const size_t height_,
1782                            const std::string &map_,
1783                            const StorageType type_,
1784                            const void *pixels_ )
1785 {
1786   ExceptionInfo exceptionInfo;
1787   GetExceptionInfo( &exceptionInfo );
1788   MagickCore::Image* image =
1789     ConstituteImage( width_, height_, map_.c_str(), type_, pixels_,
1790                      &exceptionInfo );
1791   replaceImage( image );
1792   throwException( exceptionInfo );
1793   (void) DestroyExceptionInfo( &exceptionInfo );
1794 }
1795
1796 // Reduce noise in image
1797 void Magick::Image::reduceNoise ( const double order_ )
1798 {
1799   ExceptionInfo exceptionInfo;
1800   GetExceptionInfo( &exceptionInfo );
1801   MagickCore::Image* newImage =
1802     StatisticImage( image(), NonpeakStatistic, (size_t) order_, (size_t) order_,
1803     &exceptionInfo );
1804   replaceImage( newImage );
1805   throwException( exceptionInfo );
1806   (void) DestroyExceptionInfo( &exceptionInfo );
1807 }
1808
1809 // Resize image
1810 void Magick::Image::resize( const Geometry &geometry_ )
1811 {
1812   // Calculate new size.  This code should be supported using binary arguments
1813   // in the ImageMagick library.
1814   ssize_t x = 0;
1815   ssize_t y = 0;
1816   size_t width = columns();
1817   size_t height = rows();
1818
1819   ParseMetaGeometry (static_cast<std::string>(geometry_).c_str(),
1820                      &x, &y,
1821                      &width, &height );
1822
1823   ExceptionInfo exceptionInfo;
1824   GetExceptionInfo( &exceptionInfo );
1825   MagickCore::Image* newImage =
1826     ResizeImage( image(),
1827                width,
1828                height,
1829                image()->filter,
1830                &exceptionInfo);
1831   replaceImage( newImage );
1832   throwException( exceptionInfo );
1833   (void) DestroyExceptionInfo( &exceptionInfo );
1834 }
1835
1836 // Roll image
1837 void Magick::Image::roll ( const Geometry &roll_ )
1838 {
1839   ssize_t xOff = roll_.xOff();
1840   if ( roll_.xNegative() )
1841     xOff = 0 - xOff;
1842   ssize_t yOff = roll_.yOff();
1843   if ( roll_.yNegative() )
1844     yOff = 0 - yOff;
1845
1846   ExceptionInfo exceptionInfo;
1847   GetExceptionInfo( &exceptionInfo );
1848   MagickCore::Image* newImage =
1849     RollImage( image(), xOff, yOff, &exceptionInfo );
1850   replaceImage( newImage );
1851   throwException( exceptionInfo );
1852   (void) DestroyExceptionInfo( &exceptionInfo );
1853 }
1854 void Magick::Image::roll ( const size_t columns_,
1855                            const size_t rows_ )
1856 {
1857   ExceptionInfo exceptionInfo;
1858   GetExceptionInfo( &exceptionInfo );
1859   MagickCore::Image* newImage =
1860     RollImage( image(),
1861                static_cast<ssize_t>(columns_),
1862                static_cast<ssize_t>(rows_), &exceptionInfo );
1863   replaceImage( newImage );
1864   throwException( exceptionInfo );
1865   (void) DestroyExceptionInfo( &exceptionInfo );
1866 }
1867
1868 // Rotate image
1869 void Magick::Image::rotate ( const double degrees_ )
1870 {
1871   ExceptionInfo exceptionInfo;
1872   GetExceptionInfo( &exceptionInfo );
1873   MagickCore::Image* newImage =
1874     RotateImage( image(), degrees_, &exceptionInfo);
1875   replaceImage( newImage );
1876   throwException( exceptionInfo );
1877   (void) DestroyExceptionInfo( &exceptionInfo );
1878 }
1879
1880 // Sample image
1881 void Magick::Image::sample ( const Geometry &geometry_ )
1882 {
1883   ssize_t x = 0;
1884   ssize_t y = 0;
1885   size_t width = columns();
1886   size_t height = rows();
1887
1888   ParseMetaGeometry (static_cast<std::string>(geometry_).c_str(),
1889                       &x, &y,
1890                       &width, &height );
1891
1892   ExceptionInfo exceptionInfo;
1893   GetExceptionInfo( &exceptionInfo );
1894   MagickCore::Image* newImage =
1895     SampleImage( image(), width, height, &exceptionInfo );
1896   replaceImage( newImage );
1897   throwException( exceptionInfo );
1898   (void) DestroyExceptionInfo( &exceptionInfo );
1899 }
1900
1901 // Scale image
1902 void Magick::Image::scale ( const Geometry &geometry_ )
1903 {
1904   ssize_t x = 0;
1905   ssize_t y = 0;
1906   size_t width = columns();
1907   size_t height = rows();
1908
1909   ParseMetaGeometry (static_cast<std::string>(geometry_).c_str(),
1910                       &x, &y,
1911                       &width, &height );
1912
1913   ExceptionInfo exceptionInfo;
1914   GetExceptionInfo( &exceptionInfo );
1915   MagickCore::Image* newImage =
1916     ScaleImage( image(), width, height, &exceptionInfo );
1917   replaceImage( newImage );
1918   throwException( exceptionInfo );
1919   (void) DestroyExceptionInfo( &exceptionInfo );
1920 }
1921
1922 // Segment (coalesce similar image components) by analyzing the
1923 // histograms of the color components and identifying units that are
1924 // homogeneous with the fuzzy c-means technique.
1925 void Magick::Image::segment ( const double clusterThreshold_, 
1926                               const double smoothingThreshold_ )
1927 {
1928   ExceptionInfo exceptionInfo;
1929   GetExceptionInfo( &exceptionInfo );
1930   modifyImage();
1931   SegmentImage ( image(),
1932                  options()->quantizeColorSpace(),
1933                  (MagickBooleanType) options()->verbose(),
1934                  clusterThreshold_,
1935                  smoothingThreshold_, &exceptionInfo );
1936   SyncImage( image(), &exceptionInfo );
1937   throwException( exceptionInfo );
1938   (void) DestroyExceptionInfo( &exceptionInfo );
1939 }
1940
1941 // Shade image using distant light source
1942 void Magick::Image::shade ( const double azimuth_,
1943                             const double elevation_,
1944                             const bool   colorShading_ )
1945 {
1946   ExceptionInfo exceptionInfo;
1947   GetExceptionInfo( &exceptionInfo );
1948   MagickCore::Image* newImage =
1949     ShadeImage( image(),
1950                 colorShading_ == true ? MagickTrue : MagickFalse,
1951                 azimuth_,
1952                 elevation_,
1953                 &exceptionInfo);
1954   replaceImage( newImage );
1955   throwException( exceptionInfo );
1956   (void) DestroyExceptionInfo( &exceptionInfo );
1957 }
1958
1959 // Simulate an image shadow
1960 void Magick::Image::shadow( const double percent_opacity_, const double sigma_,
1961   const ssize_t x_, const ssize_t y_ )
1962 {
1963   ExceptionInfo exceptionInfo;
1964   GetExceptionInfo( &exceptionInfo );
1965   MagickCore::Image* newImage = ShadowImage( image(), percent_opacity_, sigma_,
1966     x_, y_, &exceptionInfo );
1967   replaceImage( newImage );
1968   throwException( exceptionInfo );
1969   (void) DestroyExceptionInfo( &exceptionInfo );
1970 }
1971
1972 // Sharpen pixels in image
1973 void Magick::Image::sharpen ( const double radius_, const double sigma_ )
1974 {
1975   ExceptionInfo exceptionInfo;
1976   GetExceptionInfo( &exceptionInfo );
1977   MagickCore::Image* newImage =
1978     SharpenImage( image(),
1979                   radius_,
1980                   sigma_,
1981                   &exceptionInfo );
1982   replaceImage( newImage );
1983   throwException( exceptionInfo );
1984   (void) DestroyExceptionInfo( &exceptionInfo );
1985 }
1986
1987 void Magick::Image::sharpenChannel ( const ChannelType channel_,
1988                                      const double radius_, const double sigma_ )
1989 {
1990   ExceptionInfo exceptionInfo;
1991   GetExceptionInfo( &exceptionInfo );
1992   ChannelType channel_mask = SetPixelChannelMask( image(), channel_ );
1993   MagickCore::Image* newImage =
1994     SharpenImage( image(),
1995                          radius_,
1996                          sigma_,
1997                          &exceptionInfo );
1998   (void) SetPixelChannelMapMask( image(), channel_mask );
1999   replaceImage( newImage );
2000   throwException( exceptionInfo );
2001   (void) DestroyExceptionInfo( &exceptionInfo );
2002 }
2003
2004 // Shave pixels from image edges.
2005 void Magick::Image::shave ( const Geometry &geometry_ )
2006 {
2007   RectangleInfo shaveInfo = geometry_;
2008   ExceptionInfo exceptionInfo;
2009   GetExceptionInfo( &exceptionInfo );
2010   MagickCore::Image* newImage =
2011     ShaveImage( image(),
2012                &shaveInfo,
2013                &exceptionInfo);
2014   replaceImage( newImage );
2015   throwException( exceptionInfo );
2016   (void) DestroyExceptionInfo( &exceptionInfo );
2017 }
2018
2019 // Shear image
2020 void Magick::Image::shear ( const double xShearAngle_,
2021                             const double yShearAngle_ )
2022 {
2023   ExceptionInfo exceptionInfo;
2024   GetExceptionInfo( &exceptionInfo );
2025   MagickCore::Image* newImage =
2026     ShearImage( image(),
2027                 xShearAngle_,
2028                 yShearAngle_,
2029                 &exceptionInfo );
2030   replaceImage( newImage );
2031   throwException( exceptionInfo );
2032   (void) DestroyExceptionInfo( &exceptionInfo );
2033 }
2034
2035 // Contrast image
2036 void Magick::Image::sigmoidalContrast ( const size_t sharpen_, const double contrast, const double midpoint )
2037 {
2038   ExceptionInfo exceptionInfo;
2039   GetExceptionInfo( &exceptionInfo );
2040   modifyImage();
2041   (void) SigmoidalContrastImage( image(), (MagickBooleanType) sharpen_, contrast, midpoint, &exceptionInfo );
2042   throwException( exceptionInfo );
2043   (void) DestroyExceptionInfo( &exceptionInfo );
2044 }
2045
2046 // Solarize image (similar to effect seen when exposing a photographic
2047 // film to light during the development process)
2048 void Magick::Image::solarize ( const double factor_ )
2049 {
2050   ExceptionInfo exceptionInfo;
2051   GetExceptionInfo( &exceptionInfo );
2052   modifyImage();
2053   SolarizeImage ( image(), factor_, &exceptionInfo );
2054   throwException( exceptionInfo );
2055   (void) DestroyExceptionInfo( &exceptionInfo );
2056 }
2057
2058 // Sparse color image, given a set of coordinates, interpolates the colors
2059 // found at those coordinates, across the whole image, using various methods.
2060 //
2061 void Magick::Image::sparseColor ( const ChannelType channel,
2062                                   const SparseColorMethod method,
2063                                   const size_t number_arguments,
2064                                   const double *arguments )
2065 {
2066   ExceptionInfo exceptionInfo;
2067   GetExceptionInfo( &exceptionInfo );
2068
2069   ChannelType channel_mask = SetPixelChannelMask( image(), channel );
2070   MagickCore::Image* newImage = SparseColorImage ( image(), method,
2071     number_arguments, arguments, &exceptionInfo );
2072   (void) SetPixelChannelMapMask( image(), channel_mask );
2073   replaceImage( newImage );
2074   throwException( exceptionInfo );
2075   (void) DestroyExceptionInfo( &exceptionInfo );
2076 }
2077
2078 // Spread pixels randomly within image by specified ammount
2079 void Magick::Image::spread ( const size_t amount_ )
2080 {
2081   ExceptionInfo exceptionInfo;
2082   GetExceptionInfo( &exceptionInfo );
2083   MagickCore::Image* newImage =
2084     SpreadImage( image(),
2085                  amount_,
2086                  image()->interpolate,
2087                  &exceptionInfo );
2088   replaceImage( newImage );
2089   throwException( exceptionInfo );
2090   (void) DestroyExceptionInfo( &exceptionInfo );
2091 }
2092
2093 // Add a digital watermark to the image (based on second image)
2094 void Magick::Image::stegano ( const Image &watermark_ )
2095 {
2096   ExceptionInfo exceptionInfo;
2097   GetExceptionInfo( &exceptionInfo );
2098   MagickCore::Image* newImage =
2099     SteganoImage( image(),
2100                   watermark_.constImage(),
2101                   &exceptionInfo);
2102   replaceImage( newImage );
2103   throwException( exceptionInfo );
2104   (void) DestroyExceptionInfo( &exceptionInfo );
2105 }
2106
2107 // Stereo image (left image is current image)
2108 void Magick::Image::stereo ( const Image &rightImage_ )
2109 {
2110   ExceptionInfo exceptionInfo;
2111   GetExceptionInfo( &exceptionInfo );
2112   MagickCore::Image* newImage =
2113     StereoImage( image(),
2114                  rightImage_.constImage(),
2115                  &exceptionInfo);
2116   replaceImage( newImage );
2117   throwException( exceptionInfo );
2118   (void) DestroyExceptionInfo( &exceptionInfo );
2119 }
2120
2121 // Swirl image
2122 void Magick::Image::swirl ( const double degrees_ )
2123 {
2124   ExceptionInfo exceptionInfo;
2125   GetExceptionInfo( &exceptionInfo );
2126   MagickCore::Image* newImage =
2127     SwirlImage( image(), degrees_, image()->interpolate,
2128                 &exceptionInfo);
2129   replaceImage( newImage );
2130   throwException( exceptionInfo );
2131   (void) DestroyExceptionInfo( &exceptionInfo );
2132 }
2133
2134 // Texture image
2135 void Magick::Image::texture ( const Image &texture_ )
2136 {
2137   modifyImage();
2138   ExceptionInfo exceptionInfo;
2139   GetExceptionInfo( &exceptionInfo );
2140   TextureImage( image(), texture_.constImage(), &exceptionInfo );
2141   throwException( exceptionInfo );
2142   (void) DestroyExceptionInfo( &exceptionInfo );
2143 }
2144
2145 // Threshold image
2146 void Magick::Image::threshold ( const double threshold_ )
2147 {
2148   modifyImage();
2149   ExceptionInfo exceptionInfo;
2150   GetExceptionInfo( &exceptionInfo );
2151   BilevelImage( image(), threshold_, &exceptionInfo );
2152   throwException( exceptionInfo );
2153   (void) DestroyExceptionInfo( &exceptionInfo );
2154 }
2155
2156 // Transform image based on image geometry only
2157 void Magick::Image::transform ( const Geometry &imageGeometry_ )
2158 {
2159   modifyImage();
2160   ExceptionInfo exceptionInfo;
2161   GetExceptionInfo( &exceptionInfo );
2162   TransformImage ( &(image()), 0,
2163                    std::string(imageGeometry_).c_str(), &exceptionInfo );
2164   throwException( exceptionInfo );
2165   (void) DestroyExceptionInfo( &exceptionInfo );
2166 }
2167 // Transform image based on image and crop geometries
2168 void Magick::Image::transform ( const Geometry &imageGeometry_,
2169                                 const Geometry &cropGeometry_ )
2170 {
2171   modifyImage();
2172   ExceptionInfo exceptionInfo;
2173   GetExceptionInfo( &exceptionInfo );
2174   TransformImage ( &(image()), std::string(cropGeometry_).c_str(),
2175                    std::string(imageGeometry_).c_str(), &exceptionInfo );
2176   throwException( exceptionInfo );
2177   (void) DestroyExceptionInfo( &exceptionInfo );
2178 }
2179
2180 // Add matte image to image, setting pixels matching color to transparent
2181 void Magick::Image::transparent ( const Color &color_ )
2182 {
2183   if ( !color_.isValid() )
2184   {
2185     throwExceptionExplicit( OptionError,
2186                             "Color argument is invalid" );
2187   }
2188
2189   std::string color = color_;
2190
2191   PixelInfo target;
2192   ExceptionInfo exceptionInfo;
2193   GetExceptionInfo( &exceptionInfo );
2194   (void) QueryColorCompliance(std::string(color_).c_str(),AllCompliance,
2195     &target,&exceptionInfo);
2196   modifyImage();
2197   TransparentPaintImage ( image(), &target, TransparentAlpha, MagickFalse,
2198     &exceptionInfo );
2199   throwException( exceptionInfo );
2200   (void) DestroyExceptionInfo( &exceptionInfo );
2201 }
2202
2203 // Add matte image to image, setting pixels matching color to transparent
2204 void Magick::Image::transparentChroma(const Color &colorLow_,
2205   const Color &colorHigh_)
2206 {
2207   if ( !colorLow_.isValid() || !colorHigh_.isValid() )
2208   {
2209     throwExceptionExplicit( OptionError,
2210                             "Color argument is invalid" );
2211   }
2212
2213   std::string colorLow = colorLow_;
2214   std::string colorHigh = colorHigh_;
2215
2216   PixelInfo targetLow;
2217   PixelInfo targetHigh;
2218   ExceptionInfo exceptionInfo;
2219   GetExceptionInfo( &exceptionInfo );
2220   (void) QueryColorCompliance(std::string(colorLow_).c_str(),
2221     AllCompliance,&targetLow,&exceptionInfo);
2222   (void) QueryColorCompliance(std::string(colorHigh_).c_str(),
2223     AllCompliance,&targetHigh,&exceptionInfo);
2224   modifyImage();
2225   TransparentPaintImageChroma ( image(), &targetLow, &targetHigh,
2226     TransparentAlpha, MagickFalse, &exceptionInfo );
2227   throwException( exceptionInfo );
2228   (void) DestroyExceptionInfo( &exceptionInfo );
2229 }
2230
2231
2232 // Trim edges that are the background color from the image
2233 void Magick::Image::trim ( void )
2234 {
2235   ExceptionInfo exceptionInfo;
2236   GetExceptionInfo( &exceptionInfo );
2237   MagickCore::Image* newImage =
2238     TrimImage( image(), &exceptionInfo);
2239   replaceImage( newImage );
2240   throwException( exceptionInfo );
2241   (void) DestroyExceptionInfo( &exceptionInfo );
2242 }
2243
2244 // Replace image with a sharpened version of the original image
2245 // using the unsharp mask algorithm.
2246 //  radius_
2247 //    the radius of the Gaussian, in pixels, not counting the
2248 //    center pixel.
2249 //  sigma_
2250 //    the standard deviation of the Gaussian, in pixels.
2251 //  amount_
2252 //    the percentage of the difference between the original and
2253 //    the blur image that is added back into the original.
2254 // threshold_
2255 //   the threshold in pixels needed to apply the diffence amount.
2256 void Magick::Image::unsharpmask ( const double radius_,
2257                                   const double sigma_,
2258                                   const double amount_,
2259                                   const double threshold_ )
2260 {
2261   ExceptionInfo exceptionInfo;
2262   GetExceptionInfo( &exceptionInfo );
2263   MagickCore::Image* newImage =
2264     UnsharpMaskImage( image(),
2265                       radius_,
2266                       sigma_,
2267                       amount_,
2268                       threshold_,
2269                       &exceptionInfo );
2270   replaceImage( newImage );
2271   throwException( exceptionInfo );
2272   (void) DestroyExceptionInfo( &exceptionInfo );
2273 }
2274
2275 void Magick::Image::unsharpmaskChannel ( const ChannelType channel_,
2276                                          const double radius_,
2277                                          const double sigma_,
2278                                          const double amount_,
2279                                          const double threshold_ )
2280 {
2281   ExceptionInfo exceptionInfo;
2282   GetExceptionInfo( &exceptionInfo );
2283   ChannelType channel_mask = SetPixelChannelMask( image(), channel_ );
2284   MagickCore::Image* newImage =
2285     UnsharpMaskImage( image(),
2286                              radius_,
2287                              sigma_,
2288                              amount_,
2289                              threshold_,
2290                              &exceptionInfo );
2291   (void) SetPixelChannelMapMask( image(), channel_mask );
2292   replaceImage( newImage );
2293   throwException( exceptionInfo );
2294   (void) DestroyExceptionInfo( &exceptionInfo );
2295 }
2296
2297 // Map image pixels to a sine wave
2298 void Magick::Image::wave ( const double amplitude_, const double wavelength_ )
2299 {
2300   ExceptionInfo exceptionInfo;
2301   GetExceptionInfo( &exceptionInfo );
2302   MagickCore::Image* newImage =
2303     WaveImage( image(),
2304                amplitude_,
2305                wavelength_,
2306                image()->interpolate,
2307                &exceptionInfo);
2308   replaceImage( newImage );
2309   throwException( exceptionInfo );
2310   (void) DestroyExceptionInfo( &exceptionInfo );
2311 }
2312
2313 // Write image to file
2314 void Magick::Image::write( const std::string &imageSpec_ )
2315 {
2316   ExceptionInfo exceptionInfo;
2317   GetExceptionInfo( &exceptionInfo );
2318   modifyImage();
2319   fileName( imageSpec_ );
2320   WriteImage( imageInfo(), image(), &exceptionInfo );
2321   throwException( exceptionInfo );
2322   (void) DestroyExceptionInfo( &exceptionInfo );
2323 }
2324
2325 // Write image to in-memory BLOB
2326 void Magick::Image::write ( Blob *blob_ )
2327 {
2328   modifyImage();
2329   size_t length = 2048; // Efficient size for small images
2330   ExceptionInfo exceptionInfo;
2331   GetExceptionInfo( &exceptionInfo );
2332   void* data = ImageToBlob( imageInfo(),
2333                             image(),
2334                             &length,
2335                             &exceptionInfo);
2336   throwException( exceptionInfo );
2337   blob_->updateNoCopy( data, length, Blob::MallocAllocator );
2338   throwImageException();
2339   (void) DestroyExceptionInfo( &exceptionInfo );
2340 }
2341 void Magick::Image::write ( Blob *blob_,
2342                             const std::string &magick_ )
2343 {
2344   modifyImage();
2345   magick(magick_);
2346   size_t length = 2048; // Efficient size for small images
2347   ExceptionInfo exceptionInfo;
2348   GetExceptionInfo( &exceptionInfo );
2349   void* data = ImageToBlob( imageInfo(),
2350                             image(),
2351                             &length,
2352                             &exceptionInfo);
2353   throwException( exceptionInfo );
2354   blob_->updateNoCopy( data, length, Blob::MallocAllocator );
2355   throwImageException();
2356   (void) DestroyExceptionInfo( &exceptionInfo );
2357 }
2358 void Magick::Image::write ( Blob *blob_,
2359                             const std::string &magick_,
2360                             const size_t depth_ )
2361 {
2362   modifyImage();
2363   magick(magick_);
2364   depth(depth_);
2365   size_t length = 2048; // Efficient size for small images
2366   ExceptionInfo exceptionInfo;
2367   GetExceptionInfo( &exceptionInfo );
2368   void* data = ImageToBlob( imageInfo(),
2369                             image(),
2370                             &length,
2371                             &exceptionInfo);
2372   throwException( exceptionInfo );
2373   blob_->updateNoCopy( data, length, Blob::MallocAllocator );
2374   throwImageException();
2375   (void) DestroyExceptionInfo( &exceptionInfo );
2376 }
2377
2378 // Write image to an array of pixels with storage type specified
2379 // by user (ExportImagePixels), e.g.
2380 // image.write( 0, 0, 640, 1, "RGB", 0, pixels );
2381 void Magick::Image::write ( const ssize_t x_,
2382                             const ssize_t y_,
2383                             const size_t columns_,
2384                             const size_t rows_,
2385                             const std::string &map_,
2386                             const StorageType type_,
2387                             void *pixels_ )
2388 {
2389   ExceptionInfo exceptionInfo;
2390   GetExceptionInfo( &exceptionInfo );
2391   ExportImagePixels( image(), x_, y_, columns_, rows_, map_.c_str(), type_,
2392                  pixels_,
2393     &exceptionInfo);
2394   throwException( exceptionInfo );
2395   (void) DestroyExceptionInfo( &exceptionInfo );
2396 }
2397
2398 // Zoom image
2399 void Magick::Image::zoom( const Geometry &geometry_ )
2400 {
2401   // Calculate new size.  This code should be supported using binary arguments
2402   // in the ImageMagick library.
2403   ssize_t x = 0;
2404   ssize_t y = 0;
2405   size_t width = columns();
2406   size_t height = rows();
2407
2408   ParseMetaGeometry (static_cast<std::string>(geometry_).c_str(),
2409                      &x, &y,
2410                      &width, &height );
2411
2412   ExceptionInfo exceptionInfo;
2413   GetExceptionInfo( &exceptionInfo );
2414   MagickCore::Image* newImage =
2415     ResizeImage( image(),
2416                width,
2417                height,
2418                image()->filter,
2419                &exceptionInfo);
2420   replaceImage( newImage );
2421   throwException( exceptionInfo );
2422   (void) DestroyExceptionInfo( &exceptionInfo );
2423 }
2424
2425 /*
2426  * Methods for setting image attributes
2427  *
2428  */
2429
2430 // Join images into a single multi-image file
2431 void Magick::Image::adjoin ( const bool flag_ )
2432 {
2433   modifyImage();
2434   options()->adjoin( flag_ );
2435 }
2436 bool Magick::Image::adjoin ( void ) const
2437 {
2438   return constOptions()->adjoin();
2439 }
2440
2441 // Remove pixel aliasing
2442 void Magick::Image::antiAlias( const bool flag_ )
2443 {
2444   modifyImage();
2445   options()->antiAlias( static_cast<size_t>(flag_) );
2446 }
2447 bool Magick::Image::antiAlias( void )
2448 {
2449   return static_cast<bool>( options()->antiAlias( ) );
2450 }
2451
2452 // Animation inter-frame delay
2453 void Magick::Image::animationDelay ( const size_t delay_ )
2454 {
2455   modifyImage();
2456   image()->delay = delay_;
2457 }
2458 size_t Magick::Image::animationDelay ( void ) const
2459 {
2460   return constImage()->delay;
2461 }
2462
2463 // Number of iterations to play animation
2464 void Magick::Image::animationIterations ( const size_t iterations_ )
2465 {
2466   modifyImage();
2467   image()->iterations = iterations_;
2468 }
2469 size_t Magick::Image::animationIterations ( void ) const
2470 {
2471   return constImage()->iterations;
2472 }
2473
2474 // Access/Update a named image attribute
2475 void Magick::Image::attribute ( const std::string name_,
2476                                 const std::string value_ )
2477 {
2478   modifyImage();
2479   ExceptionInfo exceptionInfo;
2480   GetExceptionInfo( &exceptionInfo );
2481   SetImageProperty( image(), name_.c_str(), value_.c_str(), &exceptionInfo );
2482   throwException( exceptionInfo );
2483   (void) DestroyExceptionInfo( &exceptionInfo );
2484 }
2485 std::string Magick::Image::attribute ( const std::string name_ )
2486 {
2487   ExceptionInfo exceptionInfo;
2488   GetExceptionInfo( &exceptionInfo );
2489   const char *value = GetImageProperty( constImage(), name_.c_str(),
2490     &exceptionInfo );
2491   throwException( exceptionInfo );
2492   (void) DestroyExceptionInfo( &exceptionInfo );
2493
2494   if ( value )
2495     return std::string( value );
2496
2497   return std::string(); // Intentionally no exception
2498 }
2499
2500 // Background color
2501 void Magick::Image::backgroundColor ( const Color &backgroundColor_ )
2502 {
2503   modifyImage();
2504
2505   if ( backgroundColor_.isValid() )
2506     {
2507       image()->background_color = backgroundColor_;
2508     }
2509   else
2510     {
2511       image()->background_color = Color();
2512     }
2513
2514   options()->backgroundColor( backgroundColor_ );
2515 }
2516 Magick::Color Magick::Image::backgroundColor ( void ) const
2517 {
2518   return constOptions()->backgroundColor( );
2519 }
2520
2521 // Background fill texture
2522 void Magick::Image::backgroundTexture ( const std::string &backgroundTexture_ )
2523 {
2524   modifyImage();
2525   options()->backgroundTexture( backgroundTexture_ );
2526 }
2527 std::string Magick::Image::backgroundTexture ( void ) const
2528 {
2529   return constOptions()->backgroundTexture( );
2530 }
2531
2532 // Original image columns
2533 size_t Magick::Image::baseColumns ( void ) const
2534 {
2535   return constImage()->magick_columns;
2536 }
2537
2538 // Original image name
2539 std::string Magick::Image::baseFilename ( void ) const
2540 {
2541   return std::string(constImage()->magick_filename);
2542 }
2543
2544 // Original image rows
2545 size_t Magick::Image::baseRows ( void ) const
2546 {
2547   return constImage()->magick_rows;
2548 }
2549
2550 // Border color
2551 void Magick::Image::borderColor ( const Color &borderColor_ )
2552 {
2553   modifyImage();
2554
2555   if ( borderColor_.isValid() )
2556     {
2557       image()->border_color = borderColor_;
2558     }
2559   else
2560     {
2561       image()->border_color = Color();
2562     }
2563
2564   options()->borderColor( borderColor_ );
2565 }
2566 Magick::Color Magick::Image::borderColor ( void ) const
2567 {
2568   return constOptions()->borderColor( );
2569 }
2570
2571 // Return smallest bounding box enclosing non-border pixels. The
2572 // current fuzz value is used when discriminating between pixels.
2573 // This is the crop bounding box used by crop(Geometry(0,0));
2574 Magick::Geometry Magick::Image::boundingBox ( void ) const
2575 {
2576   ExceptionInfo exceptionInfo;
2577   GetExceptionInfo( &exceptionInfo );
2578   RectangleInfo bbox = GetImageBoundingBox( constImage(), &exceptionInfo);
2579   throwException( exceptionInfo );
2580   (void) DestroyExceptionInfo( &exceptionInfo );
2581   return Geometry( bbox );
2582 }
2583
2584 // Text bounding-box base color
2585 void Magick::Image::boxColor ( const Color &boxColor_ )
2586 {
2587   modifyImage();
2588   options()->boxColor( boxColor_ );
2589 }
2590 Magick::Color Magick::Image::boxColor ( void ) const
2591 {
2592   return constOptions()->boxColor( );
2593 }
2594
2595 // Pixel cache threshold.  Once this threshold is exceeded, all
2596 // subsequent pixels cache operations are to/from disk.
2597 // This setting is shared by all Image objects.
2598 /* static */
2599 void Magick::Image::cacheThreshold ( const size_t threshold_ )
2600 {
2601   SetMagickResourceLimit( MemoryResource, threshold_ );
2602 }
2603
2604 void Magick::Image::chromaBluePrimary ( const double x_, const double y_ )
2605 {
2606   modifyImage();
2607   image()->chromaticity.blue_primary.x = x_;
2608   image()->chromaticity.blue_primary.y = y_;
2609 }
2610 void Magick::Image::chromaBluePrimary ( double *x_, double *y_ ) const
2611 {
2612   *x_ = constImage()->chromaticity.blue_primary.x;
2613   *y_ = constImage()->chromaticity.blue_primary.y;
2614 }
2615
2616 void Magick::Image::chromaGreenPrimary ( const double x_, const double y_ )
2617 {
2618   modifyImage();
2619   image()->chromaticity.green_primary.x = x_;
2620   image()->chromaticity.green_primary.y = y_;
2621 }
2622 void Magick::Image::chromaGreenPrimary ( double *x_, double *y_ ) const
2623 {
2624   *x_ = constImage()->chromaticity.green_primary.x;
2625   *y_ = constImage()->chromaticity.green_primary.y;
2626 }
2627
2628 void Magick::Image::chromaRedPrimary ( const double x_, const double y_ )
2629 {
2630   modifyImage();
2631   image()->chromaticity.red_primary.x = x_;
2632   image()->chromaticity.red_primary.y = y_;
2633 }
2634 void Magick::Image::chromaRedPrimary ( double *x_, double *y_ ) const
2635 {
2636   *x_ = constImage()->chromaticity.red_primary.x;
2637   *y_ = constImage()->chromaticity.red_primary.y;
2638 }
2639
2640 void Magick::Image::chromaWhitePoint ( const double x_, const double y_ )
2641 {
2642   modifyImage();
2643   image()->chromaticity.white_point.x = x_;
2644   image()->chromaticity.white_point.y = y_;
2645 }
2646 void Magick::Image::chromaWhitePoint ( double *x_, double *y_ ) const
2647 {
2648   *x_ = constImage()->chromaticity.white_point.x;
2649   *y_ = constImage()->chromaticity.white_point.y;
2650 }
2651
2652 // Set image storage class
2653 void Magick::Image::classType ( const ClassType class_ )
2654 {
2655   if ( classType() == PseudoClass && class_ == DirectClass )
2656     {
2657       // Use SyncImage to synchronize the DirectClass pixels with the
2658       // color map and then set to DirectClass type.
2659       modifyImage();
2660       ExceptionInfo exceptionInfo;
2661       GetExceptionInfo( &exceptionInfo );
2662       SyncImage( image(), &exceptionInfo );
2663       throwException( exceptionInfo );
2664       (void) DestroyExceptionInfo( &exceptionInfo );
2665       image()->colormap = (PixelInfo *)
2666         RelinquishMagickMemory( image()->colormap );
2667       image()->storage_class = static_cast<MagickCore::ClassType>(DirectClass);
2668       return;
2669     }
2670
2671   if ( classType() == DirectClass && class_ == PseudoClass )
2672     {
2673       // Quantize to create PseudoClass color map
2674       modifyImage();
2675       quantizeColors(MaxColormapSize);
2676       quantize();
2677       image()->storage_class = static_cast<MagickCore::ClassType>(PseudoClass);
2678     }
2679 }
2680
2681 // Associate a clip mask with the image. The clip mask must be the
2682 // same dimensions as the image. Pass an invalid image to unset an
2683 // existing clip mask.
2684 void Magick::Image::clipMask ( const Magick::Image & clipMask_ )
2685 {
2686   modifyImage();
2687
2688   ExceptionInfo exceptionInfo;
2689   GetExceptionInfo( &exceptionInfo );
2690   if( clipMask_.isValid() )
2691     {
2692       // Set clip mask
2693       SetImageMask( image(), clipMask_.constImage(), &exceptionInfo );
2694     }
2695   else
2696     {
2697       // Unset existing clip mask
2698       SetImageMask( image(), 0, &exceptionInfo );
2699     }
2700    throwException( exceptionInfo );
2701    (void) DestroyExceptionInfo( &exceptionInfo );
2702 }
2703 Magick::Image Magick::Image::clipMask ( void  ) const
2704 {
2705    ExceptionInfo exceptionInfo;
2706    GetExceptionInfo( &exceptionInfo );
2707    MagickCore::Image* image = GetImageMask( constImage(), &exceptionInfo );
2708    throwException( exceptionInfo );
2709    (void) DestroyExceptionInfo( &exceptionInfo );
2710    return Magick::Image( image );
2711 }
2712
2713 void Magick::Image::colorFuzz ( const double fuzz_ )
2714 {
2715   modifyImage();
2716   image()->fuzz = fuzz_;
2717   options()->colorFuzz( fuzz_ );
2718 }
2719 double Magick::Image::colorFuzz ( void ) const
2720 {
2721   return constOptions()->colorFuzz( );
2722 }
2723
2724 // Set color in colormap at index
2725 void Magick::Image::colorMap ( const size_t index_,
2726                                const Color &color_ )
2727 {
2728   MagickCore::Image* imageptr = image();
2729
2730   if (index_ > (MaxColormapSize-1) )
2731     throwExceptionExplicit( OptionError,
2732                             "Colormap index must be less than MaxColormapSize" );
2733   
2734   if ( !color_.isValid() )
2735     throwExceptionExplicit( OptionError,
2736                             "Color argument is invalid");
2737   modifyImage();
2738
2739   // Ensure that colormap size is large enough
2740   if ( colorMapSize() < (index_+1) )
2741     colorMapSize( index_ + 1 );
2742
2743   // Set color at index in colormap
2744   (imageptr->colormap)[index_] = color_;
2745 }
2746 // Return color in colormap at index
2747 Magick::Color Magick::Image::colorMap ( const size_t index_ ) const
2748 {
2749   const MagickCore::Image* imageptr = constImage();
2750
2751   if ( !imageptr->colormap )
2752     throwExceptionExplicit( OptionError,
2753                             "Image does not contain a colormap");
2754
2755   if ( index_ > imageptr->colors-1 )
2756     throwExceptionExplicit( OptionError,
2757                             "Index out of range");
2758
2759   return Magick::Color( (imageptr->colormap)[index_] );
2760 }
2761
2762 // Colormap size (number of colormap entries)
2763 void Magick::Image::colorMapSize ( const size_t entries_ )
2764 {
2765   if (entries_ >MaxColormapSize )
2766     throwExceptionExplicit( OptionError,
2767                             "Colormap entries must not exceed MaxColormapSize" );
2768
2769   modifyImage();
2770
2771   MagickCore::Image* imageptr = image();
2772
2773   if( !imageptr->colormap )
2774     {
2775       // Allocate colormap
2776       imageptr->colormap =
2777         static_cast<PixelInfo*>(AcquireMagickMemory(entries_*sizeof(PixelInfo)));
2778       imageptr->colors = 0;
2779     }
2780   else if ( entries_ > imageptr->colors )
2781     {
2782       // Re-allocate colormap
2783       imageptr->colormap=(PixelInfo *)
2784         ResizeMagickMemory(imageptr->colormap,(entries_)*sizeof(PixelInfo));
2785     }
2786
2787   // Initialize any new colormap entries as all black
2788   Color black(0,0,0);
2789   for( size_t i=imageptr->colors; i<(entries_-1); i++ )
2790     (imageptr->colormap)[i] = black;
2791
2792   imageptr->colors = entries_;
2793 }
2794 size_t Magick::Image::colorMapSize ( void )
2795 {
2796   const MagickCore::Image* imageptr = constImage();
2797
2798   if ( !imageptr->colormap )
2799     throwExceptionExplicit( OptionError,
2800                             "Image does not contain a colormap");
2801
2802   return imageptr->colors;
2803 }
2804
2805 // Image colorspace
2806 void Magick::Image::colorSpace( const ColorspaceType colorSpace_ )
2807 {
2808   if ( image()->colorspace == colorSpace_ )
2809     return;
2810
2811   modifyImage();
2812   ExceptionInfo exceptionInfo;
2813   GetExceptionInfo( &exceptionInfo );
2814   TransformImageColorspace(image(), colorSpace_, &exceptionInfo);
2815   throwException( exceptionInfo );
2816   (void) DestroyExceptionInfo( &exceptionInfo );
2817 }
2818 Magick::ColorspaceType Magick::Image::colorSpace ( void ) const
2819 {
2820   return constImage()->colorspace;
2821 }
2822
2823 // Set image colorspace type.
2824 void Magick::Image::colorspaceType( const ColorspaceType colorSpace_ )
2825 {
2826   modifyImage();
2827   ExceptionInfo exceptionInfo;
2828   GetExceptionInfo( &exceptionInfo );
2829   SetImageColorspace(image(), colorSpace_, &exceptionInfo);
2830   throwException( exceptionInfo );
2831   (void) DestroyExceptionInfo( &exceptionInfo );
2832   options()->colorspaceType( colorSpace_ );
2833 }
2834 Magick::ColorspaceType Magick::Image::colorspaceType ( void ) const
2835 {
2836   return constOptions()->colorspaceType();
2837 }
2838
2839
2840 // Comment string
2841 void Magick::Image::comment ( const std::string &comment_ )
2842 {
2843   modifyImage();
2844   ExceptionInfo exceptionInfo;
2845   GetExceptionInfo( &exceptionInfo );
2846   SetImageProperty( image(), "Comment", NULL, &exceptionInfo );
2847   if ( comment_.length() > 0 )
2848     SetImageProperty( image(), "Comment", comment_.c_str(), &exceptionInfo );
2849   throwException( exceptionInfo );
2850   (void) DestroyExceptionInfo( &exceptionInfo );
2851 }
2852 std::string Magick::Image::comment ( void ) const
2853 {
2854   ExceptionInfo exceptionInfo;
2855   GetExceptionInfo( &exceptionInfo );
2856   const char *value = GetImageProperty( constImage(), "Comment",
2857     &exceptionInfo );
2858   throwException( exceptionInfo );
2859   (void) DestroyExceptionInfo( &exceptionInfo );
2860
2861   if ( value )
2862     return std::string( value );
2863
2864   return std::string(); // Intentionally no exception
2865 }
2866
2867 // Composition operator to be used when composition is implicitly used
2868 // (such as for image flattening).
2869 void Magick::Image::compose (const CompositeOperator compose_)
2870 {
2871   image()->compose=compose_;
2872 }
2873
2874 Magick::CompositeOperator Magick::Image::compose ( void ) const
2875 {
2876   return constImage()->compose;
2877 }
2878
2879 // Compression algorithm
2880 void Magick::Image::compressType ( const CompressionType compressType_ )
2881 {
2882   modifyImage();
2883   image()->compression = compressType_;
2884   options()->compressType( compressType_ );
2885 }
2886 Magick::CompressionType Magick::Image::compressType ( void ) const
2887 {
2888   return constImage()->compression;
2889 }
2890
2891 // Enable printing of debug messages from ImageMagick
2892 void Magick::Image::debug ( const bool flag_ )
2893 {
2894   modifyImage();
2895   options()->debug( flag_ );
2896 }
2897 bool Magick::Image::debug ( void ) const
2898 {
2899   return constOptions()->debug();
2900 }
2901
2902 // Tagged image format define (set/access coder-specific option) The
2903 // magick_ option specifies the coder the define applies to.  The key_
2904 // option provides the key specific to that coder.  The value_ option
2905 // provides the value to set (if any). See the defineSet() method if the
2906 // key must be removed entirely.
2907 void Magick::Image::defineValue ( const std::string &magick_,
2908                                   const std::string &key_,
2909                                   const std::string &value_ )
2910 {
2911   modifyImage();
2912   std::string format = magick_ + ":" + key_;
2913   std::string option = value_;
2914   (void) SetImageOption ( imageInfo(), format.c_str(), option.c_str() );
2915 }
2916 std::string Magick::Image::defineValue ( const std::string &magick_,
2917                                          const std::string &key_ ) const
2918 {
2919   std::string definition = magick_ + ":" + key_;
2920   const char *option =
2921     GetImageOption ( constImageInfo(), definition.c_str() );
2922   if (option)
2923     return std::string( option );
2924   return std::string( );
2925 }
2926
2927 // Tagged image format define. Similar to the defineValue() method
2928 // except that passing the flag_ value 'true' creates a value-less
2929 // define with that format and key. Passing the flag_ value 'false'
2930 // removes any existing matching definition. The method returns 'true'
2931 // if a matching key exists, and 'false' if no matching key exists.
2932 void Magick::Image::defineSet ( const std::string &magick_,
2933                                 const std::string &key_,
2934                                 bool flag_ )
2935 {
2936   modifyImage();
2937   std::string definition = magick_ + ":" + key_;
2938   if (flag_)
2939     {
2940       (void) SetImageOption ( imageInfo(), definition.c_str(),  "" );
2941     }
2942   else
2943     {
2944       DeleteImageOption( imageInfo(), definition.c_str() );
2945     }
2946 }
2947 bool Magick::Image::defineSet ( const std::string &magick_,
2948                                 const std::string &key_ ) const
2949 {
2950   std::string key = magick_ + ":" + key_;
2951   const char *option =
2952     GetImageOption ( constImageInfo(), key.c_str() );
2953   if (option)
2954     return true;
2955   return false;
2956 }
2957
2958 // Pixel resolution
2959 void Magick::Image::density ( const Geometry &density_ )
2960 {
2961   modifyImage();
2962   options()->density( density_ );
2963   if ( density_.isValid() )
2964     {
2965       image()->resolution.x = density_.width();
2966       if ( density_.height() != 0 )
2967         {
2968           image()->resolution.y = density_.height();
2969         }
2970       else
2971         {
2972           image()->resolution.y = density_.width();
2973         }
2974     }
2975   else
2976     {
2977       // Reset to default
2978       image()->resolution.x = 0;
2979       image()->resolution.y = 0;
2980     }
2981 }
2982 Magick::Geometry Magick::Image::density ( void ) const
2983 {
2984   if (isValid())
2985     {
2986       ssize_t x_resolution=72;
2987       ssize_t y_resolution=72;
2988
2989       if (constImage()->resolution.x > 0.0)
2990         x_resolution=static_cast<ssize_t>(constImage()->resolution.x + 0.5);
2991
2992       if (constImage()->resolution.y > 0.0)
2993         y_resolution=static_cast<ssize_t>(constImage()->resolution.y + 0.5);
2994
2995       return Geometry(x_resolution,y_resolution);
2996     }
2997
2998   return constOptions()->density( );
2999 }
3000
3001 // Image depth (bits allocated to red/green/blue components)
3002 void Magick::Image::depth ( const size_t depth_ )
3003 {
3004   size_t depth = depth_;
3005
3006   if (depth > MAGICKCORE_QUANTUM_DEPTH)
3007     depth=MAGICKCORE_QUANTUM_DEPTH;
3008
3009   modifyImage();
3010   image()->depth=depth;
3011   options()->depth( depth );
3012 }
3013 size_t Magick::Image::depth ( void ) const
3014 {
3015   return constImage()->depth;
3016 }
3017
3018 std::string Magick::Image::directory ( void ) const
3019 {
3020   if ( constImage()->directory )
3021     return std::string( constImage()->directory );
3022
3023   throwExceptionExplicit( CorruptImageWarning,
3024                           "Image does not contain a directory");
3025
3026   return std::string();
3027 }
3028
3029 // Endianness (little like Intel or big like SPARC) for image
3030 // formats which support endian-specific options.
3031 void Magick::Image::endian ( const Magick::EndianType endian_ )
3032 {
3033   modifyImage();
3034   options()->endian( endian_ );
3035   image()->endian = endian_;
3036 }
3037 Magick::EndianType Magick::Image::endian ( void ) const
3038 {
3039   return constImage()->endian;
3040 }
3041
3042 // EXIF profile (BLOB)
3043 void Magick::Image::exifProfile( const Magick::Blob &exifProfile_ )
3044 {
3045   modifyImage();
3046   if ( exifProfile_.data() != 0 )
3047     {
3048       StringInfo * exif_profile = AcquireStringInfo( exifProfile_.length() );
3049       SetStringInfoDatum(exif_profile ,(unsigned char *) exifProfile_.data());
3050       ExceptionInfo exceptionInfo;
3051       GetExceptionInfo( &exceptionInfo );
3052       (void) SetImageProfile( image(), "exif", exif_profile, &exceptionInfo);
3053       exif_profile =DestroyStringInfo( exif_profile );
3054       throwException( exceptionInfo );
3055       (void) DestroyExceptionInfo( &exceptionInfo );
3056     }
3057 }
3058 Magick::Blob Magick::Image::exifProfile( void ) const
3059 {
3060   const StringInfo * exif_profile = GetImageProfile( constImage(), "exif" );
3061   if ( exif_profile == (StringInfo *) NULL)
3062     return Blob( 0, 0 );
3063   return Blob(GetStringInfoDatum(exif_profile),GetStringInfoLength(exif_profile));
3064
3065
3066 // Image file name
3067 void Magick::Image::fileName ( const std::string &fileName_ )
3068 {
3069   modifyImage();
3070
3071   fileName_.copy( image()->filename,
3072                   sizeof(image()->filename) - 1 );
3073   image()->filename[ fileName_.length() ] = 0; // Null terminate
3074   
3075   options()->fileName( fileName_ );
3076   
3077 }
3078 std::string Magick::Image::fileName ( void ) const
3079 {
3080   return constOptions()->fileName( );
3081 }
3082
3083 // Image file size
3084 off_t Magick::Image::fileSize ( void ) const
3085 {
3086   return (off_t) GetBlobSize( constImage() );
3087 }
3088
3089 // Color to use when drawing inside an object
3090 void Magick::Image::fillColor ( const Magick::Color &fillColor_ )
3091 {
3092   modifyImage();
3093   options()->fillColor(fillColor_);
3094 }
3095 Magick::Color Magick::Image::fillColor ( void ) const
3096 {
3097   return constOptions()->fillColor();
3098 }
3099
3100 // Rule to use when filling drawn objects
3101 void Magick::Image::fillRule ( const Magick::FillRule &fillRule_ )
3102 {
3103   modifyImage();
3104   options()->fillRule(fillRule_);
3105 }
3106 Magick::FillRule Magick::Image::fillRule ( void ) const
3107 {
3108   return constOptions()->fillRule();
3109 }
3110
3111 // Pattern to use while filling drawn objects.
3112 void Magick::Image::fillPattern ( const Image &fillPattern_ )
3113 {
3114   modifyImage();
3115   if(fillPattern_.isValid())
3116     options()->fillPattern( fillPattern_.constImage() );
3117   else
3118     options()->fillPattern( static_cast<MagickCore::Image*>(NULL) );
3119 }
3120 Magick::Image  Magick::Image::fillPattern ( void  ) const
3121 {
3122   // FIXME: This is inordinately innefficient
3123   Image texture;
3124   
3125   const MagickCore::Image* tmpTexture = constOptions()->fillPattern( );
3126
3127   if ( tmpTexture )
3128     {
3129       ExceptionInfo exceptionInfo;
3130       GetExceptionInfo( &exceptionInfo );
3131       MagickCore::Image* image =
3132         CloneImage( tmpTexture,
3133                     0, // columns
3134                     0, // rows
3135                     MagickTrue, // orphan
3136                     &exceptionInfo);
3137       texture.replaceImage( image );
3138       throwException( exceptionInfo );
3139       (void) DestroyExceptionInfo( &exceptionInfo );
3140     }
3141   return texture;
3142 }
3143
3144 // Filter used by zoom
3145 void Magick::Image::filterType ( const Magick::FilterTypes filterType_ )
3146 {
3147   modifyImage();
3148   image()->filter = filterType_;
3149 }
3150 Magick::FilterTypes Magick::Image::filterType ( void ) const
3151 {
3152   return constImage()->filter;
3153 }
3154
3155 // Font name
3156 void Magick::Image::font ( const std::string &font_ )
3157 {
3158   modifyImage();
3159   options()->font( font_ );
3160 }
3161 std::string Magick::Image::font ( void ) const
3162 {
3163   return constOptions()->font( );
3164 }
3165
3166 // Font point size
3167 void Magick::Image::fontPointsize ( const double pointSize_ )
3168 {
3169   modifyImage();
3170   options()->fontPointsize( pointSize_ );
3171 }
3172 double Magick::Image::fontPointsize ( void ) const
3173 {
3174   return constOptions()->fontPointsize( );
3175 }
3176
3177 // Font type metrics
3178 void Magick::Image::fontTypeMetrics( const std::string &text_,
3179                                      TypeMetric *metrics )
3180 {
3181   DrawInfo *drawInfo = options()->drawInfo();
3182   drawInfo->text = const_cast<char *>(text_.c_str());
3183   ExceptionInfo exceptionInfo;
3184   GetExceptionInfo( &exceptionInfo );
3185   GetTypeMetrics( image(), drawInfo, &(metrics->_typeMetric), &exceptionInfo );
3186   drawInfo->text = 0;
3187   throwException( exceptionInfo );
3188   (void) DestroyExceptionInfo( &exceptionInfo );
3189 }
3190
3191 // Image format string
3192 std::string Magick::Image::format ( void ) const
3193 {
3194   ExceptionInfo exceptionInfo;
3195   GetExceptionInfo( &exceptionInfo );
3196   const MagickInfo * magick_info
3197     = GetMagickInfo( constImage()->magick, &exceptionInfo);
3198   throwException( exceptionInfo );
3199   (void) DestroyExceptionInfo( &exceptionInfo );
3200
3201   if (( magick_info != 0 ) && 
3202       ( *magick_info->description != '\0' ))
3203     return std::string(magick_info->description);
3204
3205   throwExceptionExplicit( CorruptImageWarning,
3206                           "Unrecognized image magick type" );
3207   return std::string();
3208 }
3209
3210 // Gamma adjustment
3211 double Magick::Image::gamma ( void ) const
3212 {
3213   return constImage()->gamma;
3214 }
3215
3216 Magick::Geometry Magick::Image::geometry ( void ) const
3217 {
3218   if ( constImage()->geometry )
3219   {
3220     return Geometry(constImage()->geometry);
3221   }
3222
3223   throwExceptionExplicit( OptionWarning,
3224                           "Image does not contain a geometry");
3225
3226   return Geometry();
3227 }
3228
3229 void Magick::Image::gifDisposeMethod ( const size_t disposeMethod_ )
3230 {
3231   modifyImage();
3232   image()->dispose = (DisposeType) disposeMethod_;
3233 }
3234 size_t Magick::Image::gifDisposeMethod ( void ) const
3235 {
3236   // FIXME: It would be better to return an enumeration
3237   return constImage()->dispose;
3238 }
3239
3240 // ICC ICM color profile (BLOB)
3241 void Magick::Image::iccColorProfile( const Magick::Blob &colorProfile_ )
3242 {
3243   profile("icm",colorProfile_);
3244 }
3245 Magick::Blob Magick::Image::iccColorProfile( void ) const
3246 {
3247   const StringInfo * color_profile = GetImageProfile( constImage(), "icc" );
3248   if ( color_profile == (StringInfo *) NULL)
3249     return Blob( 0, 0 );
3250   return Blob( GetStringInfoDatum(color_profile), GetStringInfoLength(color_profile) );
3251 }
3252
3253 void Magick::Image::interlaceType ( const Magick::InterlaceType interlace_ )
3254 {
3255   modifyImage();
3256   image()->interlace = interlace_;
3257   options()->interlaceType ( interlace_ );
3258 }
3259 Magick::InterlaceType Magick::Image::interlaceType ( void ) const
3260 {
3261   return constImage()->interlace;
3262 }
3263
3264 // IPTC profile (BLOB)
3265 void Magick::Image::iptcProfile( const Magick::Blob &iptcProfile_ )
3266 {
3267   modifyImage();
3268   if (  iptcProfile_.data() != 0 )
3269     {
3270       StringInfo * iptc_profile = AcquireStringInfo( iptcProfile_.length() );
3271       SetStringInfoDatum(iptc_profile ,(unsigned char *) iptcProfile_.data());
3272       ExceptionInfo exceptionInfo;
3273       GetExceptionInfo( &exceptionInfo );
3274       (void) SetImageProfile( image(), "iptc", iptc_profile, &exceptionInfo);
3275       iptc_profile =DestroyStringInfo( iptc_profile );
3276       throwException( exceptionInfo );
3277       (void) DestroyExceptionInfo( &exceptionInfo );
3278     }
3279 }
3280 Magick::Blob Magick::Image::iptcProfile( void ) const
3281 {
3282   const StringInfo * iptc_profile = GetImageProfile( constImage(), "iptc" );
3283   if ( iptc_profile == (StringInfo *) NULL)
3284     return Blob( 0, 0 );
3285   return Blob( GetStringInfoDatum(iptc_profile), GetStringInfoLength(iptc_profile));
3286 }
3287
3288 // Does object contain valid image?
3289 void Magick::Image::isValid ( const bool isValid_ )
3290 {
3291   if ( !isValid_ )
3292     {
3293       delete _imgRef;
3294       _imgRef = new ImageRef;
3295     }
3296   else if ( !isValid() )
3297     {
3298       // Construct with single-pixel black image to make
3299       // image valid.  This is an obvious hack.
3300       size( Geometry(1,1) );
3301       read( "xc:#000000" );
3302     }
3303 }
3304
3305 bool Magick::Image::isValid ( void ) const
3306 {
3307   if ( rows() && columns() )
3308     return true;
3309
3310   return false;
3311 }
3312
3313 // Label image
3314 void Magick::Image::label ( const std::string &label_ )
3315 {
3316   modifyImage();
3317   ExceptionInfo exceptionInfo;
3318   GetExceptionInfo( &exceptionInfo );
3319   SetImageProperty ( image(), "Label", NULL, &exceptionInfo );
3320   if ( label_.length() > 0 )
3321     SetImageProperty ( image(), "Label", label_.c_str(), &exceptionInfo );
3322   throwException( exceptionInfo );
3323   (void) DestroyExceptionInfo( &exceptionInfo );
3324 }
3325 std::string Magick::Image::label ( void ) const
3326 {
3327   ExceptionInfo exceptionInfo;
3328   GetExceptionInfo( &exceptionInfo );
3329   const char *value = GetImageProperty( constImage(), "Label", &exceptionInfo );
3330   throwException( exceptionInfo );
3331   (void) DestroyExceptionInfo( &exceptionInfo );
3332
3333   if ( value )
3334     return std::string( value );
3335
3336   return std::string();
3337 }
3338
3339 void Magick::Image::magick ( const std::string &magick_ )
3340 {
3341   modifyImage();
3342
3343   magick_.copy( image()->magick,
3344                 sizeof(image()->magick) - 1 );
3345   image()->magick[ magick_.length() ] = 0;
3346   
3347   options()->magick( magick_ );
3348 }
3349 std::string Magick::Image::magick ( void ) const
3350 {
3351   if ( *(constImage()->magick) != '\0' )
3352     return std::string(constImage()->magick);
3353
3354   return constOptions()->magick( );
3355 }
3356
3357 void Magick::Image::matte ( const bool matteFlag_ )
3358 {
3359   modifyImage();
3360
3361   // If matte channel is requested, but image doesn't already have a
3362   // matte channel, then create an opaque matte channel.  Likewise, if
3363   // the image already has a matte channel but a matte channel is not
3364   // desired, then set the matte channel to opaque.
3365   ExceptionInfo exceptionInfo;
3366   GetExceptionInfo( &exceptionInfo );
3367   if ((matteFlag_ && !constImage()->matte) ||
3368       (constImage()->matte && !matteFlag_))
3369     SetImageAlpha(image(),OpaqueAlpha,&exceptionInfo);
3370   throwException( exceptionInfo );
3371   (void) DestroyExceptionInfo( &exceptionInfo );
3372
3373   image()->matte = (MagickBooleanType) matteFlag_;
3374 }
3375 bool Magick::Image::matte ( void ) const
3376 {
3377   if ( constImage()->matte )
3378     return true;
3379   else
3380     return false;
3381 }
3382
3383 void Magick::Image::matteColor ( const Color &matteColor_ )
3384 {
3385   modifyImage();
3386   
3387   if ( matteColor_.isValid() )
3388     {
3389       image()->matte_color = matteColor_;
3390       options()->matteColor( matteColor_ );
3391     }
3392   else
3393     {
3394       // Set to default matte color
3395       Color tmpColor( "#BDBDBD" );
3396       image()->matte_color = tmpColor;
3397       options()->matteColor( tmpColor );
3398     }
3399 }
3400 Magick::Color Magick::Image::matteColor ( void ) const
3401 {
3402   return Color( ClampToQuantum( constImage()->matte_color.red ),
3403                 ClampToQuantum( constImage()->matte_color.green ),
3404                 ClampToQuantum( constImage()->matte_color.blue ) );
3405 }
3406
3407 double Magick::Image::meanErrorPerPixel ( void ) const
3408 {
3409   return(constImage()->error.mean_error_per_pixel);
3410 }
3411
3412 // Image modulus depth (minimum number of bits required to support
3413 // red/green/blue components without loss of accuracy)
3414 void Magick::Image::modulusDepth ( const size_t depth_ )
3415 {
3416   modifyImage();
3417   ExceptionInfo exceptionInfo;
3418   GetExceptionInfo( &exceptionInfo );
3419   SetImageDepth( image(), depth_, &exceptionInfo );
3420   throwException( exceptionInfo );
3421   (void) DestroyExceptionInfo( &exceptionInfo );
3422   options()->depth( depth_ );
3423 }
3424 size_t Magick::Image::modulusDepth ( void ) const
3425 {
3426   ExceptionInfo exceptionInfo;
3427   GetExceptionInfo( &exceptionInfo );
3428   size_t depth=GetImageDepth( constImage(), &exceptionInfo );
3429   throwException( exceptionInfo );
3430   (void) DestroyExceptionInfo( &exceptionInfo );
3431   return depth;
3432 }
3433
3434 void Magick::Image::monochrome ( const bool monochromeFlag_ )
3435 {
3436   modifyImage();
3437   options()->monochrome( monochromeFlag_ );
3438 }
3439 bool Magick::Image::monochrome ( void ) const
3440 {
3441   return constOptions()->monochrome( );
3442 }
3443
3444 Magick::Geometry Magick::Image::montageGeometry ( void ) const
3445 {
3446   if ( constImage()->montage )
3447     return Magick::Geometry(constImage()->montage);
3448
3449   throwExceptionExplicit( CorruptImageWarning,
3450                           "Image does not contain a montage" );
3451
3452   return Magick::Geometry();
3453 }
3454
3455 double Magick::Image::normalizedMaxError ( void ) const
3456 {
3457   return(constImage()->error.normalized_maximum_error);
3458 }
3459
3460 double Magick::Image::normalizedMeanError ( void ) const
3461 {
3462   return constImage()->error.normalized_mean_error;
3463 }
3464
3465 // Image orientation
3466 void Magick::Image::orientation ( const Magick::OrientationType orientation_ )
3467 {
3468   modifyImage();
3469   image()->orientation = orientation_;
3470 }
3471 Magick::OrientationType Magick::Image::orientation ( void ) const
3472 {
3473   return constImage()->orientation;
3474 }
3475
3476 void Magick::Image::penColor ( const Color &penColor_ )
3477 {
3478   modifyImage();
3479   options()->fillColor(penColor_);
3480   options()->strokeColor(penColor_);
3481 }
3482 Magick::Color Magick::Image::penColor ( void  ) const
3483 {
3484   return constOptions()->fillColor();
3485 }
3486
3487 void Magick::Image::penTexture ( const Image &penTexture_ )
3488 {
3489   modifyImage();
3490   if(penTexture_.isValid())
3491     options()->fillPattern( penTexture_.constImage() );
3492   else
3493     options()->fillPattern( static_cast<MagickCore::Image*>(NULL) );
3494 }
3495
3496 Magick::Image  Magick::Image::penTexture ( void  ) const
3497 {
3498   // FIXME: This is inordinately innefficient
3499   Image texture;
3500   
3501   const MagickCore::Image* tmpTexture = constOptions()->fillPattern( );
3502
3503   if ( tmpTexture )
3504     {
3505       ExceptionInfo exceptionInfo;
3506       GetExceptionInfo( &exceptionInfo );
3507       MagickCore::Image* image =
3508         CloneImage( tmpTexture,
3509                     0, // columns
3510                     0, // rows
3511                     MagickTrue, // orphan
3512                     &exceptionInfo);
3513       texture.replaceImage( image );
3514       throwException( exceptionInfo );
3515   (void) DestroyExceptionInfo( &exceptionInfo );
3516     }
3517   return texture;
3518 }
3519
3520 // Set the color of a pixel.
3521 void Magick::Image::pixelColor ( const ssize_t x_, const ssize_t y_,
3522                                  const Color &color_ )
3523 {
3524   // Test arguments to ensure they are within the image.
3525   if ( y_ > (ssize_t) rows() || x_ > (ssize_t) columns() )
3526     throwExceptionExplicit( OptionError,
3527             "Access outside of image boundary" );
3528       
3529   modifyImage();
3530
3531   // Set image to DirectClass
3532   classType( DirectClass );
3533
3534   // Get pixel view
3535   Pixels pixels(*this);
3536     // Set pixel value
3537   Quantum *pixel = pixels.get(x_, y_, 1, 1 );
3538   PixelInfo packet = color_;
3539   MagickCore::SetPixelInfoPixel(constImage(),&packet,pixel);
3540   // Tell ImageMagick that pixels have been updated
3541   pixels.sync();
3542
3543   return;
3544 }
3545
3546 // Get the color of a pixel
3547 Magick::Color Magick::Image::pixelColor ( const ssize_t x_,
3548                                           const ssize_t y_ ) const
3549 {
3550   const Quantum* pixel = getConstPixels( x_, y_, 1, 1 );
3551   if ( pixel )
3552     {
3553       PixelInfo packet;
3554       MagickCore::GetPixelInfoPixel(constImage(),pixel,&packet);
3555       return Color( packet );
3556     }
3557
3558   return Color(); // invalid
3559 }
3560
3561 // Preferred size and location of an image canvas.
3562 void Magick::Image::page ( const Magick::Geometry &pageSize_ )
3563 {
3564   modifyImage();
3565   options()->page( pageSize_ );
3566   image()->page = pageSize_;
3567 }
3568 Magick::Geometry Magick::Image::page ( void ) const
3569 {
3570   return Geometry( constImage()->page.width,
3571                    constImage()->page.height,
3572                    AbsoluteValue(constImage()->page.x),
3573                    AbsoluteValue(constImage()->page.y),
3574                    constImage()->page.x < 0 ? true : false,
3575                    constImage()->page.y < 0 ? true : false);
3576 }
3577
3578 // Add a named profile to an image or remove a named profile by
3579 // passing an empty Blob (use default Blob constructor).
3580 // Valid names are:
3581 // "*", "8BIM", "ICM", "IPTC", or a generic profile name.
3582 void Magick::Image::profile( const std::string name_,
3583                              const Magick::Blob &profile_ )
3584 {
3585   modifyImage();
3586   ExceptionInfo exceptionInfo;
3587   GetExceptionInfo( &exceptionInfo );
3588   ssize_t result = ProfileImage( image(), name_.c_str(),
3589                              (unsigned char *)profile_.data(),
3590                              profile_.length(), &exceptionInfo);
3591   (void) result;
3592   throwException( exceptionInfo );
3593   (void) DestroyExceptionInfo( &exceptionInfo );
3594
3595 }
3596
3597 // Retrieve a named profile from the image.
3598 // Valid names are:
3599 // "8BIM", "8BIMTEXT", "APP1", "APP1JPEG", "ICC", "ICM", & "IPTC" or
3600 // an existing generic profile name.
3601 Magick::Blob Magick::Image::profile( const std::string name_ ) const
3602 {
3603   const MagickCore::Image* image = constImage();
3604                                                                                 
3605   const StringInfo * profile = GetImageProfile( image, name_.c_str() );
3606                                                                                 
3607   if ( profile != (StringInfo *) NULL)
3608       return Blob( (void*) GetStringInfoDatum(profile), GetStringInfoLength(profile));
3609                                                                                 
3610   Blob blob;
3611   Image temp_image = *this;
3612   temp_image.write( &blob, name_ );
3613   return blob;
3614 }
3615
3616 void Magick::Image::quality ( const size_t quality_ )
3617 {
3618   modifyImage();
3619   image()->quality = quality_;
3620   options()->quality( quality_ );
3621 }
3622 size_t Magick::Image::quality ( void ) const
3623 {
3624   return constImage()->quality;
3625 }
3626
3627 void Magick::Image::quantizeColors ( const size_t colors_ )
3628 {
3629   modifyImage();
3630   options()->quantizeColors( colors_ );
3631 }
3632 size_t Magick::Image::quantizeColors ( void ) const
3633 {
3634   return constOptions()->quantizeColors( );
3635 }
3636
3637 void Magick::Image::quantizeColorSpace
3638   ( const Magick::ColorspaceType colorSpace_ )
3639 {
3640   modifyImage();
3641   options()->quantizeColorSpace( colorSpace_ );
3642 }
3643 Magick::ColorspaceType Magick::Image::quantizeColorSpace ( void ) const
3644 {
3645   return constOptions()->quantizeColorSpace( );
3646 }
3647
3648 void Magick::Image::quantizeDither ( const bool ditherFlag_ )
3649 {
3650   modifyImage();
3651   options()->quantizeDither( ditherFlag_ );
3652 }
3653 bool Magick::Image::quantizeDither ( void ) const
3654 {
3655   return constOptions()->quantizeDither( );
3656 }
3657
3658 void Magick::Image::quantizeTreeDepth ( const size_t treeDepth_ )
3659 {
3660   modifyImage();
3661   options()->quantizeTreeDepth( treeDepth_ );
3662 }
3663 size_t Magick::Image::quantizeTreeDepth ( void ) const
3664 {
3665   return constOptions()->quantizeTreeDepth( );
3666 }
3667
3668 void Magick::Image::renderingIntent
3669   ( const Magick::RenderingIntent renderingIntent_ )
3670 {
3671   modifyImage();
3672   image()->rendering_intent = renderingIntent_;
3673 }
3674 Magick::RenderingIntent Magick::Image::renderingIntent ( void ) const
3675 {
3676   return static_cast<Magick::RenderingIntent>(constImage()->rendering_intent);
3677 }
3678
3679 void Magick::Image::resolutionUnits
3680   ( const Magick::ResolutionType resolutionUnits_ )
3681 {
3682   modifyImage();
3683   image()->units = resolutionUnits_;
3684   options()->resolutionUnits( resolutionUnits_ );
3685 }
3686 Magick::ResolutionType Magick::Image::resolutionUnits ( void ) const
3687 {
3688   return constOptions()->resolutionUnits( );
3689 }
3690
3691 void Magick::Image::scene ( const size_t scene_ )
3692 {
3693   modifyImage();
3694   image()->scene = scene_;
3695 }
3696 size_t Magick::Image::scene ( void ) const
3697 {
3698   return constImage()->scene;
3699 }
3700
3701 std::string Magick::Image::signature ( const bool force_ ) const
3702 {
3703   Lock( &_imgRef->_mutexLock );
3704
3705   // Re-calculate image signature if necessary
3706   ExceptionInfo exceptionInfo;
3707   GetExceptionInfo( &exceptionInfo );
3708   if ( force_ ||
3709        !GetImageProperty(constImage(), "Signature", &exceptionInfo) ||
3710        constImage()->taint )
3711     {
3712       SignatureImage( const_cast<MagickCore::Image *>(constImage()), &exceptionInfo );
3713     }
3714
3715   const char *property = GetImageProperty(constImage(), "Signature",
3716     &exceptionInfo);
3717   throwException( exceptionInfo );
3718   (void) DestroyExceptionInfo( &exceptionInfo );
3719
3720   return std::string( property );
3721 }
3722
3723 void Magick::Image::size ( const Geometry &geometry_ )
3724 {
3725   modifyImage();
3726   options()->size( geometry_ );
3727   image()->rows = geometry_.height();
3728   image()->columns = geometry_.width();
3729 }
3730 Magick::Geometry Magick::Image::size ( void ) const
3731 {
3732   return Magick::Geometry( constImage()->columns, constImage()->rows );
3733 }
3734
3735 // Splice image
3736 void Magick::Image::splice( const Geometry &geometry_ )
3737 {
3738   RectangleInfo spliceInfo = geometry_;
3739   ExceptionInfo exceptionInfo;
3740   GetExceptionInfo( &exceptionInfo );
3741   MagickCore::Image* newImage =
3742     SpliceImage( image(), &spliceInfo, &exceptionInfo);
3743   replaceImage( newImage );
3744   throwException( exceptionInfo );
3745   (void) DestroyExceptionInfo( &exceptionInfo );
3746 }
3747
3748 // Obtain image statistics. Statistics are normalized to the range of
3749 // 0.0 to 1.0 and are output to the specified ImageStatistics
3750 // structure.
3751 void Magick::Image::statistics ( ImageStatistics *statistics ) 
3752 {
3753   double
3754     maximum,
3755     minimum;
3756
3757   ExceptionInfo exceptionInfo;
3758   GetExceptionInfo( &exceptionInfo );
3759
3760   ChannelType channel_mask = SetPixelChannelMask( image(), RedChannel);
3761   (void) GetImageRange( image(),&minimum,&maximum,&exceptionInfo);
3762   statistics->red.minimum=minimum;
3763   statistics->red.maximum=maximum;
3764   (void) GetImageMean( image(),&statistics->red.mean,
3765     &statistics->red.standard_deviation,&exceptionInfo);
3766   (void) GetImageKurtosis( image(),&statistics->red.kurtosis,
3767     &statistics->red.skewness,&exceptionInfo);
3768   (void) SetPixelChannelMapMask( image(), channel_mask );
3769
3770   channel_mask = SetPixelChannelMask( image(), GreenChannel);
3771   (void) GetImageRange( image(),&minimum,&maximum,&exceptionInfo);
3772   statistics->green.minimum=minimum;
3773   statistics->green.maximum=maximum;
3774   (void) GetImageMean( image(),&statistics->green.mean,
3775     &statistics->green.standard_deviation,&exceptionInfo);
3776   (void) GetImageKurtosis( image(),&statistics->green.kurtosis,
3777     &statistics->green.skewness,&exceptionInfo);
3778   (void) SetPixelChannelMapMask( image(), channel_mask );
3779
3780   channel_mask = SetPixelChannelMask( image(), GreenChannel);
3781   (void) GetImageRange( image(),&minimum,&maximum,&exceptionInfo);
3782   statistics->blue.minimum=minimum;
3783   statistics->blue.maximum=maximum;
3784   (void) GetImageMean( image(),&statistics->blue.mean,
3785     &statistics->blue.standard_deviation,&exceptionInfo);
3786   (void) GetImageKurtosis( image(),&statistics->blue.kurtosis,
3787     &statistics->blue.skewness,&exceptionInfo);
3788   (void) SetPixelChannelMapMask( image(), channel_mask );
3789
3790   channel_mask = SetPixelChannelMask( image(), AlphaChannel);
3791   (void) GetImageRange( image(),&minimum,&maximum,&exceptionInfo);
3792   statistics->alpha.minimum=minimum;
3793   statistics->alpha.maximum=maximum;
3794   (void) GetImageMean( image(),&statistics->alpha.mean,
3795     &statistics->alpha.standard_deviation,&exceptionInfo);
3796   (void) GetImageKurtosis( image(),&statistics->alpha.kurtosis,
3797     &statistics->alpha.skewness,&exceptionInfo);
3798   (void) SetPixelChannelMapMask( image(), channel_mask );
3799
3800   throwException( exceptionInfo );
3801   (void) DestroyExceptionInfo( &exceptionInfo );
3802 }
3803
3804 // Strip strips an image of all profiles and comments.
3805 void Magick::Image::strip ( void )
3806 {
3807   modifyImage();
3808   ExceptionInfo exceptionInfo;
3809   GetExceptionInfo( &exceptionInfo );
3810   StripImage( image(), &exceptionInfo );
3811   throwException( exceptionInfo );
3812   (void) DestroyExceptionInfo( &exceptionInfo );
3813 }
3814
3815 // enabled/disable stroke anti-aliasing
3816 void Magick::Image::strokeAntiAlias ( const bool flag_ )
3817 {
3818   modifyImage();
3819   options()->strokeAntiAlias(flag_);
3820 }
3821 bool Magick::Image::strokeAntiAlias ( void ) const
3822 {
3823   return constOptions()->strokeAntiAlias();
3824 }
3825
3826 // Color to use when drawing object outlines
3827 void Magick::Image::strokeColor ( const Magick::Color &strokeColor_ )
3828 {
3829   modifyImage();
3830   options()->strokeColor(strokeColor_);
3831 }
3832 Magick::Color Magick::Image::strokeColor ( void ) const
3833 {
3834   return constOptions()->strokeColor();
3835 }
3836
3837 // dash pattern for drawing vector objects (default one)
3838 void Magick::Image::strokeDashArray ( const double* strokeDashArray_ )
3839 {
3840   modifyImage();
3841   options()->strokeDashArray( strokeDashArray_ );
3842 }
3843
3844 const double* Magick::Image::strokeDashArray ( void ) const
3845 {
3846   return constOptions()->strokeDashArray( );
3847 }
3848
3849 // dash offset for drawing vector objects (default one)
3850 void Magick::Image::strokeDashOffset ( const double strokeDashOffset_ )
3851 {
3852   modifyImage();
3853   options()->strokeDashOffset( strokeDashOffset_ );
3854 }
3855
3856 double Magick::Image::strokeDashOffset ( void ) const
3857 {
3858   return constOptions()->strokeDashOffset( );
3859 }
3860
3861 // Specify the shape to be used at the end of open subpaths when they
3862 // are stroked. Values of LineCap are UndefinedCap, ButtCap, RoundCap,
3863 // and SquareCap.
3864 void Magick::Image::strokeLineCap ( const Magick::LineCap lineCap_ )
3865 {
3866   modifyImage();
3867   options()->strokeLineCap( lineCap_ );
3868 }
3869 Magick::LineCap Magick::Image::strokeLineCap ( void ) const
3870 {
3871   return constOptions()->strokeLineCap( );
3872 }
3873
3874 // Specify the shape to be used at the corners of paths (or other
3875 // vector shapes) when they are stroked. Values of LineJoin are
3876 // UndefinedJoin, MiterJoin, RoundJoin, and BevelJoin.
3877 void Magick::Image::strokeLineJoin ( const Magick::LineJoin lineJoin_ )
3878 {
3879   modifyImage();
3880   options()->strokeLineJoin( lineJoin_ );
3881 }
3882 Magick::LineJoin Magick::Image::strokeLineJoin ( void ) const
3883 {
3884   return constOptions()->strokeLineJoin( );
3885 }
3886
3887 // Specify miter limit. When two line segments meet at a sharp angle
3888 // and miter joins have been specified for 'lineJoin', it is possible
3889 // for the miter to extend far beyond the thickness of the line
3890 // stroking the path. The miterLimit' imposes a limit on the ratio of
3891 // the miter length to the 'lineWidth'. The default value of this
3892 // parameter is 4.
3893 void Magick::Image::strokeMiterLimit ( const size_t strokeMiterLimit_ )
3894 {
3895   modifyImage();
3896   options()->strokeMiterLimit( strokeMiterLimit_ );
3897 }
3898 size_t Magick::Image::strokeMiterLimit ( void ) const
3899 {
3900   return constOptions()->strokeMiterLimit( );
3901 }
3902
3903 // Pattern to use while stroking drawn objects.
3904 void Magick::Image::strokePattern ( const Image &strokePattern_ )
3905 {
3906   modifyImage();
3907   if(strokePattern_.isValid())
3908     options()->strokePattern( strokePattern_.constImage() );
3909   else
3910     options()->strokePattern( static_cast<MagickCore::Image*>(NULL) );
3911 }
3912 Magick::Image  Magick::Image::strokePattern ( void  ) const
3913 {
3914   // FIXME: This is inordinately innefficient
3915   Image texture;
3916   
3917   const MagickCore::Image* tmpTexture = constOptions()->strokePattern( );
3918
3919   if ( tmpTexture )
3920     {
3921       ExceptionInfo exceptionInfo;
3922       GetExceptionInfo( &exceptionInfo );
3923       MagickCore::Image* image =
3924         CloneImage( tmpTexture,
3925                     0, // columns
3926                     0, // rows
3927                     MagickTrue, // orphan
3928                     &exceptionInfo);
3929       throwException( exceptionInfo );
3930       (void) DestroyExceptionInfo( &exceptionInfo );
3931       texture.replaceImage( image );
3932     }
3933   return texture;
3934 }
3935
3936 // Stroke width for drawing lines, circles, ellipses, etc.
3937 void Magick::Image::strokeWidth ( const double strokeWidth_ )
3938 {
3939   modifyImage();
3940   options()->strokeWidth( strokeWidth_ );
3941 }
3942 double Magick::Image::strokeWidth ( void ) const
3943 {
3944   return constOptions()->strokeWidth( );
3945 }
3946
3947 void Magick::Image::subImage ( const size_t subImage_ )
3948 {
3949   modifyImage();
3950   options()->subImage( subImage_ );
3951 }
3952 size_t Magick::Image::subImage ( void ) const
3953 {
3954   return constOptions()->subImage( );
3955 }
3956
3957 void Magick::Image::subRange ( const size_t subRange_ )
3958 {
3959   modifyImage();
3960   options()->subRange( subRange_ );
3961 }
3962 size_t Magick::Image::subRange ( void ) const
3963 {
3964   return constOptions()->subRange( );
3965 }
3966
3967 // Annotation text encoding (e.g. "UTF-16")
3968 void Magick::Image::textEncoding ( const std::string &encoding_ )
3969 {
3970   modifyImage();
3971   options()->textEncoding( encoding_ );
3972 }
3973 std::string Magick::Image::textEncoding ( void ) const
3974 {
3975   return constOptions()->textEncoding( );
3976 }
3977
3978 size_t Magick::Image::totalColors ( void )
3979 {
3980   ExceptionInfo exceptionInfo;
3981   GetExceptionInfo( &exceptionInfo );
3982   size_t colors = GetNumberColors( image(), 0, &exceptionInfo);
3983   throwException( exceptionInfo );
3984   (void) DestroyExceptionInfo( &exceptionInfo );
3985   return colors;
3986 }
3987
3988 // Origin of coordinate system to use when annotating with text or drawing
3989 void Magick::Image::transformOrigin ( const double x_, const double y_ )
3990 {
3991   modifyImage();
3992   options()->transformOrigin( x_, y_ );
3993 }
3994
3995 // Rotation to use when annotating with text or drawing
3996 void Magick::Image::transformRotation ( const double angle_ )
3997 {
3998   modifyImage();
3999   options()->transformRotation( angle_ );
4000 }
4001
4002 // Reset transformation parameters to default
4003 void Magick::Image::transformReset ( void )
4004 {
4005   modifyImage();
4006   options()->transformReset();
4007 }
4008
4009 // Scale to use when annotating with text or drawing
4010 void Magick::Image::transformScale ( const double sx_, const double sy_ )
4011 {
4012   modifyImage();
4013   options()->transformScale( sx_, sy_ );
4014 }
4015
4016 // Skew to use in X axis when annotating with text or drawing
4017 void Magick::Image::transformSkewX ( const double skewx_ )
4018 {
4019   modifyImage();
4020   options()->transformSkewX( skewx_ );
4021 }
4022
4023 // Skew to use in Y axis when annotating with text or drawing
4024 void Magick::Image::transformSkewY ( const double skewy_ )
4025 {
4026   modifyImage();
4027   options()->transformSkewY( skewy_ );
4028 }
4029
4030 // Image representation type
4031 Magick::ImageType Magick::Image::type ( void ) const
4032 {
4033
4034   ExceptionInfo exceptionInfo;
4035   GetExceptionInfo( &exceptionInfo );
4036   ImageType image_type = constOptions()->type();
4037   if ( image_type == UndefinedType )
4038     image_type= GetImageType( constImage(), &exceptionInfo);
4039   throwException( exceptionInfo );
4040   (void) DestroyExceptionInfo( &exceptionInfo );
4041   return image_type;
4042 }
4043 void Magick::Image::type ( const Magick::ImageType type_)
4044 {
4045   ExceptionInfo exceptionInfo;
4046   GetExceptionInfo( &exceptionInfo );
4047   modifyImage();
4048   options()->type( type_ );
4049   SetImageType( image(), type_, &exceptionInfo );
4050   throwException( exceptionInfo );
4051   (void) DestroyExceptionInfo( &exceptionInfo );
4052 }
4053
4054 void Magick::Image::verbose ( const bool verboseFlag_ )
4055 {
4056   modifyImage();
4057   options()->verbose( verboseFlag_ );
4058 }
4059 bool Magick::Image::verbose ( void ) const
4060 {
4061   return constOptions()->verbose( );
4062 }
4063
4064 void Magick::Image::view ( const std::string &view_ )
4065 {
4066   modifyImage();
4067   options()->view( view_ );
4068 }
4069 std::string Magick::Image::view ( void ) const
4070 {
4071   return constOptions()->view( );
4072 }
4073
4074 // Virtual pixel method
4075 void Magick::Image::virtualPixelMethod ( const VirtualPixelMethod virtual_pixel_method_ )
4076 {
4077   modifyImage();
4078   ExceptionInfo exceptionInfo;
4079   GetExceptionInfo( &exceptionInfo );
4080   SetImageVirtualPixelMethod( image(), virtual_pixel_method_, &exceptionInfo );
4081   throwException( exceptionInfo );
4082   (void) DestroyExceptionInfo( &exceptionInfo );
4083 }
4084 Magick::VirtualPixelMethod Magick::Image::virtualPixelMethod ( void ) const
4085 {
4086   return GetImageVirtualPixelMethod( constImage() );
4087 }
4088
4089 void Magick::Image::x11Display ( const std::string &display_ )
4090 {
4091   modifyImage();
4092   options()->x11Display( display_ );
4093 }
4094 std::string Magick::Image::x11Display ( void ) const
4095 {
4096   return constOptions()->x11Display( );
4097 }
4098
4099 double Magick::Image::xResolution ( void ) const
4100 {
4101   return constImage()->resolution.x;
4102 }
4103 double Magick::Image::yResolution ( void ) const
4104 {
4105   return constImage()->resolution.y;
4106 }
4107
4108 // Copy Constructor
4109 Magick::Image::Image( const Image & image_ )
4110   : _imgRef(image_._imgRef)
4111 {
4112   Lock( &_imgRef->_mutexLock );
4113
4114   // Increase reference count
4115   ++_imgRef->_refCount;
4116 }
4117
4118 // Assignment operator
4119 Magick::Image& Magick::Image::operator=( const Magick::Image &image_ )
4120 {
4121   if( this != &image_ )
4122     {
4123       {
4124         Lock( &image_._imgRef->_mutexLock );
4125         ++image_._imgRef->_refCount;
4126       }
4127
4128       bool doDelete = false;
4129       {
4130         Lock( &_imgRef->_mutexLock );
4131         if ( --_imgRef->_refCount == 0 )
4132           doDelete = true;
4133       }
4134
4135       if ( doDelete )
4136         {
4137           // Delete old image reference with associated image and options.
4138           delete _imgRef;
4139           _imgRef = 0;
4140         }
4141       // Use new image reference
4142       _imgRef = image_._imgRef;
4143     }
4144
4145   return *this;
4146 }
4147
4148 //////////////////////////////////////////////////////////////////////    
4149 //
4150 // Low-level Pixel Access Routines
4151 //
4152 // Also see the Pixels class, which provides support for multiple
4153 // cache views. The low-level pixel access routines in the Image
4154 // class are provided in order to support backward compatability.
4155 //
4156 //////////////////////////////////////////////////////////////////////
4157
4158 // Transfers read-only pixels from the image to the pixel cache as
4159 // defined by the specified region
4160 const Magick::Quantum* Magick::Image::getConstPixels
4161   ( const ssize_t x_, const ssize_t y_,
4162     const size_t columns_,
4163     const size_t rows_ ) const
4164 {
4165   ExceptionInfo exceptionInfo;
4166   GetExceptionInfo( &exceptionInfo );
4167   const Quantum* p = (*GetVirtualPixels)( constImage(),
4168                                                 x_, y_,
4169                                                 columns_, rows_,
4170                                                 &exceptionInfo );
4171   throwException( exceptionInfo );
4172   (void) DestroyExceptionInfo( &exceptionInfo );
4173   return p;
4174 }
4175
4176 // Obtain read-only pixel associated pixels channels
4177 const void* Magick::Image::getConstMetacontent ( void ) const
4178 {
4179   const void* result = GetVirtualMetacontent( constImage() );
4180
4181   if( !result )
4182     throwImageException();
4183
4184   return result;
4185 }
4186
4187 // Obtain image pixel associated pixels channels
4188 void* Magick::Image::getMetacontent ( void )
4189 {
4190   void* result = GetAuthenticMetacontent( image() );
4191
4192   if( !result )
4193     throwImageException();
4194
4195   return ( result );
4196 }
4197
4198 // Transfers pixels from the image to the pixel cache as defined
4199 // by the specified region. Modified pixels may be subsequently
4200 // transferred back to the image via syncPixels.
4201 Magick::Quantum* Magick::Image::getPixels ( const ssize_t x_, const ssize_t y_,
4202                                                 const size_t columns_,
4203                                                 const size_t rows_ )
4204 {
4205   modifyImage();
4206   ExceptionInfo exceptionInfo;
4207   GetExceptionInfo( &exceptionInfo );
4208   Quantum* result = (*GetAuthenticPixels)( image(),
4209                                            x_, y_,
4210                                            columns_, rows_, &exceptionInfo );
4211   throwException( exceptionInfo );
4212   (void) DestroyExceptionInfo( &exceptionInfo );
4213
4214   return result;
4215 }
4216
4217 // Allocates a pixel cache region to store image pixels as defined
4218 // by the region rectangle.  This area is subsequently transferred
4219 // from the pixel cache to the image via syncPixels.
4220 Magick::Quantum* Magick::Image::setPixels ( const ssize_t x_, const ssize_t y_,
4221                                                 const size_t columns_,
4222                                                 const size_t rows_ )
4223 {
4224   modifyImage();
4225   ExceptionInfo exceptionInfo;
4226   GetExceptionInfo( &exceptionInfo );
4227   Quantum* result = (*QueueAuthenticPixels)( image(),
4228                                            x_, y_,
4229                                            columns_, rows_, &exceptionInfo );
4230   throwException( exceptionInfo );
4231   (void) DestroyExceptionInfo( &exceptionInfo );
4232
4233   return result;
4234 }
4235
4236 // Transfers the image cache pixels to the image.
4237 void Magick::Image::syncPixels ( void )
4238 {
4239   ExceptionInfo exceptionInfo;
4240   GetExceptionInfo( &exceptionInfo );
4241   (*SyncAuthenticPixels)( image(), &exceptionInfo );
4242   throwException( exceptionInfo );
4243   (void) DestroyExceptionInfo( &exceptionInfo );
4244 }
4245
4246 // Transfers one or more pixel components from a buffer or file
4247 // into the image pixel cache of an image.
4248 // Used to support image decoders.
4249 void Magick::Image::readPixels ( const Magick::QuantumType quantum_,
4250                                  const unsigned char *source_ )
4251 {
4252   QuantumInfo
4253     *quantum_info;
4254
4255   quantum_info=AcquireQuantumInfo(imageInfo(),image());
4256   ExceptionInfo exceptionInfo;
4257   GetExceptionInfo( &exceptionInfo );
4258   ImportQuantumPixels(image(),(MagickCore::CacheView *) NULL,quantum_info,
4259     quantum_,source_, &exceptionInfo);
4260   throwException( exceptionInfo );
4261   (void) DestroyExceptionInfo( &exceptionInfo );
4262   quantum_info=DestroyQuantumInfo(quantum_info);
4263 }
4264
4265 // Transfers one or more pixel components from the image pixel
4266 // cache to a buffer or file.
4267 // Used to support image encoders.
4268 void Magick::Image::writePixels ( const Magick::QuantumType quantum_,
4269                                   unsigned char *destination_ )
4270 {
4271   QuantumInfo
4272     *quantum_info;
4273
4274   quantum_info=AcquireQuantumInfo(imageInfo(),image());
4275   ExceptionInfo exceptionInfo;
4276   GetExceptionInfo( &exceptionInfo );
4277   ExportQuantumPixels(image(),(MagickCore::CacheView *) NULL,quantum_info,
4278     quantum_,destination_, &exceptionInfo);
4279   quantum_info=DestroyQuantumInfo(quantum_info);
4280   throwException( exceptionInfo );
4281   (void) DestroyExceptionInfo( &exceptionInfo );
4282 }
4283
4284 /////////////////////////////////////////////////////////////////////
4285 //
4286 // No end-user methods beyond this point
4287 //
4288 /////////////////////////////////////////////////////////////////////
4289
4290
4291 //
4292 // Construct using existing image and default options
4293 //
4294 Magick::Image::Image ( MagickCore::Image* image_ )
4295   : _imgRef(new ImageRef( image_))
4296 {
4297 }
4298
4299 // Get Magick::Options*
4300 Magick::Options* Magick::Image::options( void )
4301 {
4302   return _imgRef->options();
4303 }
4304 const Magick::Options* Magick::Image::constOptions( void ) const
4305 {
4306   return _imgRef->options();
4307 }
4308
4309 // Get MagickCore::Image*
4310 MagickCore::Image*& Magick::Image::image( void )
4311 {
4312   return _imgRef->image();
4313 }
4314 const MagickCore::Image* Magick::Image::constImage( void ) const
4315 {
4316   return _imgRef->image();
4317 }
4318
4319 // Get ImageInfo *
4320 MagickCore::ImageInfo* Magick::Image::imageInfo( void )
4321 {
4322   return _imgRef->options()->imageInfo();
4323 }
4324 const MagickCore::ImageInfo * Magick::Image::constImageInfo( void ) const
4325 {
4326   return _imgRef->options()->imageInfo();
4327 }
4328
4329 // Get QuantizeInfo *
4330 MagickCore::QuantizeInfo* Magick::Image::quantizeInfo( void )
4331 {
4332   return _imgRef->options()->quantizeInfo();
4333 }
4334 const MagickCore::QuantizeInfo * Magick::Image::constQuantizeInfo( void ) const
4335 {
4336   return _imgRef->options()->quantizeInfo();
4337 }
4338
4339 //
4340 // Replace current image
4341 //
4342 MagickCore::Image * Magick::Image::replaceImage
4343   ( MagickCore::Image* replacement_ )
4344 {
4345   MagickCore::Image* image;
4346   
4347   if( replacement_ )
4348     image = replacement_;
4349   else
4350     {
4351       ExceptionInfo exceptionInfo;
4352       GetExceptionInfo( &exceptionInfo );
4353       image = AcquireImage(constImageInfo(), &exceptionInfo);
4354       throwException( exceptionInfo );
4355       (void) DestroyExceptionInfo( &exceptionInfo );
4356     }
4357
4358   {
4359     Lock( &_imgRef->_mutexLock );
4360
4361     if ( _imgRef->_refCount == 1 )
4362       {
4363         // We own the image, just replace it, and de-register
4364         _imgRef->id( -1 );
4365         _imgRef->image(image);
4366       }
4367     else
4368       {
4369         // We don't own the image, dereference and replace with copy
4370         --_imgRef->_refCount;
4371         _imgRef = new ImageRef( image, constOptions() );
4372       }
4373   }
4374
4375   return _imgRef->_image;
4376 }
4377
4378 //
4379 // Prepare to modify image or image options
4380 // Replace current image and options with copy if reference count > 1
4381 //
4382 void Magick::Image::modifyImage( void )
4383 {
4384   {
4385     Lock( &_imgRef->_mutexLock );
4386     if ( _imgRef->_refCount == 1 )
4387       {
4388         // De-register image and return
4389         _imgRef->id( -1 );
4390         return;
4391       }
4392   }
4393
4394   ExceptionInfo exceptionInfo;
4395   GetExceptionInfo( &exceptionInfo );
4396   replaceImage( CloneImage( image(),
4397                             0, // columns
4398                             0, // rows
4399                             MagickTrue, // orphan
4400                             &exceptionInfo) );
4401   throwException( exceptionInfo );
4402   (void) DestroyExceptionInfo( &exceptionInfo );
4403   return;
4404 }
4405
4406 //
4407 // Test for an ImageMagick reported error and throw exception if one
4408 // has been reported.  Secretly resets image->exception back to default
4409 // state even though this method is const.
4410 //
4411 void Magick::Image::throwImageException( void ) const
4412 {
4413   // Throw C++ exception while resetting Image exception to default state
4414 }
4415
4416 // Register image with image registry or obtain registration id
4417 ssize_t Magick::Image::registerId( void )
4418 {
4419   Lock( &_imgRef->_mutexLock );
4420   if( _imgRef->id() < 0 )
4421     {
4422       char id[MaxTextExtent];
4423       ExceptionInfo exceptionInfo;
4424       GetExceptionInfo( &exceptionInfo );
4425       _imgRef->id(_imgRef->id()+1);
4426       sprintf(id,"%.20g\n",(double) _imgRef->id());
4427       SetImageRegistry(ImageRegistryType, id, image(), &exceptionInfo);
4428       throwException( exceptionInfo );
4429   (void) DestroyExceptionInfo( &exceptionInfo );
4430     }
4431   return _imgRef->id();
4432 }
4433
4434 // Unregister image from image registry
4435 void Magick::Image::unregisterId( void )
4436 {
4437   modifyImage();
4438   _imgRef->id( -1 );
4439 }
4440
4441 //
4442 // Create a local wrapper around MagickCoreTerminus
4443 //
4444 namespace Magick
4445 {
4446   extern "C" {
4447     void MagickPlusPlusDestroyMagick(void);
4448   }
4449 }
4450
4451 void Magick::MagickPlusPlusDestroyMagick(void)
4452 {
4453   if (magick_initialized)
4454     {
4455       magick_initialized=false;
4456       MagickCore::MagickCoreTerminus();
4457     }
4458 }
4459
4460 // C library initialization routine
4461 void MagickPPExport Magick::InitializeMagick(const char *path_)
4462 {
4463   MagickCore::MagickCoreGenesis(path_,MagickFalse);
4464   if (!magick_initialized)
4465     magick_initialized=true;
4466 }
4467
4468 //
4469 // Cleanup class to ensure that ImageMagick singletons are destroyed
4470 // so as to avoid any resemblence to a memory leak (which seems to
4471 // confuse users)
4472 //
4473 namespace Magick
4474 {
4475
4476   class MagickCleanUp
4477   {
4478   public:
4479     MagickCleanUp( void );
4480     ~MagickCleanUp( void );
4481   };
4482
4483   // The destructor for this object is invoked when the destructors for
4484   // static objects in this translation unit are invoked.
4485   static MagickCleanUp magickCleanUpGuard;
4486 }
4487
4488 Magick::MagickCleanUp::MagickCleanUp ( void )
4489 {
4490   // Don't even think about invoking InitializeMagick here!
4491 }
4492
4493 Magick::MagickCleanUp::~MagickCleanUp ( void )
4494 {
4495   MagickPlusPlusDestroyMagick();
4496 }