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