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