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