]> granicus.if.org Git - imagemagick/blob - magick/image.c
(no commit message)
[imagemagick] / magick / image.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                     IIIII  M   M   AAA    GGGG  EEEEE                       %
7 %                       I    MM MM  A   A  G      E                           %
8 %                       I    M M M  AAAAA  G  GG  EEE                         %
9 %                       I    M   M  A   A  G   G  E                           %
10 %                     IIIII  M   M  A   A   GGGG  EEEEE                       %
11 %                                                                             %
12 %                                                                             %
13 %                           MagickCore Image Methods                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/animate.h"
45 #include "magick/artifact.h"
46 #include "magick/blob.h"
47 #include "magick/blob-private.h"
48 #include "magick/cache.h"
49 #include "magick/cache-private.h"
50 #include "magick/cache-view.h"
51 #include "magick/client.h"
52 #include "magick/color.h"
53 #include "magick/color-private.h"
54 #include "magick/colormap.h"
55 #include "magick/colorspace.h"
56 #include "magick/colorspace-private.h"
57 #include "magick/composite.h"
58 #include "magick/composite-private.h"
59 #include "magick/compress.h"
60 #include "magick/constitute.h"
61 #include "magick/deprecate.h"
62 #include "magick/display.h"
63 #include "magick/draw.h"
64 #include "magick/enhance.h"
65 #include "magick/exception.h"
66 #include "magick/exception-private.h"
67 #include "magick/gem.h"
68 #include "magick/geometry.h"
69 #include "magick/histogram.h"
70 #include "magick/image-private.h"
71 #include "magick/list.h"
72 #include "magick/magic.h"
73 #include "magick/magick.h"
74 #include "magick/memory_.h"
75 #include "magick/module.h"
76 #include "magick/monitor.h"
77 #include "magick/monitor-private.h"
78 #include "magick/option.h"
79 #include "magick/paint.h"
80 #include "magick/pixel-private.h"
81 #include "magick/profile.h"
82 #include "magick/property.h"
83 #include "magick/quantize.h"
84 #include "magick/random_.h"
85 #include "magick/segment.h"
86 #include "magick/semaphore.h"
87 #include "magick/signature-private.h"
88 #include "magick/statistic.h"
89 #include "magick/string_.h"
90 #include "magick/string-private.h"
91 #include "magick/thread-private.h"
92 #include "magick/threshold.h"
93 #include "magick/timer.h"
94 #include "magick/utility.h"
95 #include "magick/version.h"
96 #include "magick/xwindow-private.h"
97 \f
98 /*
99   Constant declaration.
100 */
101 const char
102   BackgroundColor[] = "#ffffff",  /* white */
103   BorderColor[] = "#dfdfdf",  /* gray */
104   DefaultTileFrame[] = "15x15+3+3",
105   DefaultTileGeometry[] = "120x120+4+3>",
106   DefaultTileLabel[] = "%f\n%G\n%b",
107   ForegroundColor[] = "#000",  /* black */
108   LoadImageTag[] = "Load/Image",
109   LoadImagesTag[] = "Load/Images",
110   MatteColor[] = "#bdbdbd",  /* gray */
111   PSDensityGeometry[] = "72.0x72.0",
112   PSPageGeometry[] = "612x792",
113   SaveImageTag[] = "Save/Image",
114   SaveImagesTag[] = "Save/Images",
115   TransparentColor[] = "#00000000";  /* transparent black */
116
117 const double
118   DefaultResolution = 72.0;
119 \f
120 /*
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 %                                                                             %
123 %                                                                             %
124 %                                                                             %
125 %   A c q u i r e I m a g e                                                   %
126 %                                                                             %
127 %                                                                             %
128 %                                                                             %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 %
131 %  AcquireImage() returns a pointer to an image structure initialized to
132 %  default values.
133 %
134 %  The format of the AcquireImage method is:
135 %
136 %      Image *AcquireImage(const ImageInfo *image_info)
137 %
138 %  A description of each parameter follows:
139 %
140 %    o image_info: Many of the image default values are set from this
141 %      structure.  For example, filename, compression, depth, background color,
142 %      and others.
143 %
144 */
145 MagickExport Image *AcquireImage(const ImageInfo *image_info)
146 {
147   const char
148     *option;
149
150   Image
151     *image;
152
153   MagickStatusType
154     flags;
155
156   /*
157     Allocate image structure.
158   */
159   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
160   image=(Image *) AcquireMagickMemory(sizeof(*image));
161   if (image == (Image *) NULL)
162     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
163   (void) ResetMagickMemory(image,0,sizeof(*image));
164   /*
165     Initialize Image structure.
166   */
167   (void) CopyMagickString(image->magick,"MIFF",MaxTextExtent);
168   image->storage_class=DirectClass;
169   image->depth=MAGICKCORE_QUANTUM_DEPTH;
170   image->colorspace=RGBColorspace;
171   image->interlace=NoInterlace;
172   image->ticks_per_second=UndefinedTicksPerSecond;
173   image->compose=OverCompositeOp;
174   image->blur=1.0;
175   GetExceptionInfo(&image->exception);
176   (void) QueryColorDatabase(BackgroundColor,&image->background_color,
177     &image->exception);
178   (void) QueryColorDatabase(BorderColor,&image->border_color,&image->exception);
179   (void) QueryColorDatabase(MatteColor,&image->matte_color,&image->exception);
180   (void) QueryColorDatabase(TransparentColor,&image->transparent_color,
181     &image->exception);
182   image->x_resolution=DefaultResolution;
183   image->y_resolution=DefaultResolution;
184   image->units=PixelsPerInchResolution;
185   GetTimerInfo(&image->timer);
186   image->ping=MagickFalse;
187   image->cache=AcquirePixelCache(0);
188   image->blob=CloneBlobInfo((BlobInfo *) NULL);
189   image->debug=IsEventLogging();
190   image->reference_count=1;
191   image->semaphore=AllocateSemaphoreInfo();
192   image->signature=MagickSignature;
193   if (image_info == (ImageInfo *) NULL)
194     return(image);
195   /*
196     Transfer image info.
197   */
198   SetBlobExempt(image,image_info->file != (FILE *) NULL ? MagickTrue :
199     MagickFalse);
200   (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
201   (void) CopyMagickString(image->magick_filename,image_info->filename,
202     MaxTextExtent);
203   (void) CopyMagickString(image->magick,image_info->magick,MaxTextExtent);
204   if (image_info->size != (char *) NULL)
205     {
206       (void) ParseAbsoluteGeometry(image_info->size,&image->extract_info);
207       image->columns=image->extract_info.width;
208       image->rows=image->extract_info.height;
209       image->offset=image->extract_info.x;
210       image->extract_info.x=0;
211       image->extract_info.y=0;
212     }
213   if (image_info->extract != (char *) NULL)
214     {
215       RectangleInfo
216         geometry;
217
218       flags=ParseAbsoluteGeometry(image_info->extract,&geometry);
219       if (((flags & XValue) != 0) || ((flags & YValue) != 0))
220         {
221           image->extract_info=geometry;
222           Swap(image->columns,image->extract_info.width);
223           Swap(image->rows,image->extract_info.height);
224         }
225     }
226   image->compression=image_info->compression;
227   image->quality=image_info->quality;
228   image->endian=image_info->endian;
229   image->interlace=image_info->interlace;
230   image->units=image_info->units;
231   if (image_info->density != (char *) NULL)
232     {
233       GeometryInfo
234         geometry_info;
235
236       flags=ParseGeometry(image_info->density,&geometry_info);
237       image->x_resolution=geometry_info.rho;
238       image->y_resolution=geometry_info.sigma;
239       if ((flags & SigmaValue) == 0)
240         image->y_resolution=image->x_resolution;
241     }
242   if (image_info->page != (char *) NULL)
243     {
244       char
245         *geometry;
246
247       image->page=image->extract_info;
248       geometry=GetPageGeometry(image_info->page);
249       (void) ParseAbsoluteGeometry(geometry,&image->page);
250       geometry=DestroyString(geometry);
251     }
252   if (image_info->depth != 0)
253     image->depth=image_info->depth;
254   image->dither=image_info->dither;
255   image->background_color=image_info->background_color;
256   image->border_color=image_info->border_color;
257   image->matte_color=image_info->matte_color;
258   image->transparent_color=image_info->transparent_color;
259   image->ping=image_info->ping;
260   image->progress_monitor=image_info->progress_monitor;
261   image->client_data=image_info->client_data;
262   if (image_info->cache != (void *) NULL)
263     ClonePixelCacheMethods(image->cache,image_info->cache);
264   (void) SetImageVirtualPixelMethod(image,image_info->virtual_pixel_method);
265   (void) SyncImageSettings(image_info,image);
266   option=GetImageOption(image_info,"delay");
267   if (option != (const char *) NULL)
268     {
269       GeometryInfo
270         geometry_info;
271
272       flags=ParseGeometry(option,&geometry_info);
273       if ((flags & GreaterValue) != 0)
274         {
275           if (image->delay > (size_t) floor(geometry_info.rho+0.5))
276             image->delay=(size_t) floor(geometry_info.rho+0.5);
277         }
278       else
279         if ((flags & LessValue) != 0)
280           {
281             if (image->delay < (size_t) floor(geometry_info.rho+0.5))
282               image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
283           }
284         else
285           image->delay=(size_t) floor(geometry_info.rho+0.5);
286       if ((flags & SigmaValue) != 0)
287         image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
288     }
289   option=GetImageOption(image_info,"dispose");
290   if (option != (const char *) NULL)
291     image->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
292       MagickFalse,option);
293   return(image);
294 }
295 \f
296 /*
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 %                                                                             %
299 %                                                                             %
300 %                                                                             %
301 %   A c q u i r e I m a g e I n f o                                           %
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 %
307 %  AcquireImageInfo() allocates the ImageInfo structure.
308 %
309 %  The format of the AcquireImageInfo method is:
310 %
311 %      ImageInfo *AcquireImageInfo(void)
312 %
313 */
314 MagickExport ImageInfo *AcquireImageInfo(void)
315 {
316   ImageInfo
317     *image_info;
318
319   image_info=(ImageInfo *) AcquireMagickMemory(sizeof(*image_info));
320   if (image_info == (ImageInfo *) NULL)
321     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
322   GetImageInfo(image_info);
323   return(image_info);
324 }
325 \f
326 /*
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 %                                                                             %
329 %                                                                             %
330 %                                                                             %
331 %   A c q u i r e N e x t I m a g e                                           %
332 %                                                                             %
333 %                                                                             %
334 %                                                                             %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 %
337 %  AcquireNextImage() initializes the next image in a sequence to
338 %  default values.  The next member of image points to the newly allocated
339 %  image.  If there is a memory shortage, next is assigned NULL.
340 %
341 %  The format of the AcquireNextImage method is:
342 %
343 %      void AcquireNextImage(const ImageInfo *image_info,Image *image)
344 %
345 %  A description of each parameter follows:
346 %
347 %    o image_info: Many of the image default values are set from this
348 %      structure.  For example, filename, compression, depth, background color,
349 %      and others.
350 %
351 %    o image: the image.
352 %
353 */
354 MagickExport void AcquireNextImage(const ImageInfo *image_info,Image *image)
355 {
356   /*
357     Allocate image structure.
358   */
359   assert(image != (Image *) NULL);
360   assert(image->signature == MagickSignature);
361   if (image->debug != MagickFalse)
362     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
363   image->next=AcquireImage(image_info);
364   if (GetNextImageInList(image) == (Image *) NULL)
365     return;
366   (void) CopyMagickString(GetNextImageInList(image)->filename,image->filename,
367     MaxTextExtent);
368   if (image_info != (ImageInfo *) NULL)
369     (void) CopyMagickString(GetNextImageInList(image)->filename,
370       image_info->filename,MaxTextExtent);
371   DestroyBlob(GetNextImageInList(image));
372   image->next->blob=ReferenceBlob(image->blob);
373   image->next->endian=image->endian;
374   image->next->scene=image->scene+1;
375   image->next->previous=image;
376 }
377 \f
378 /*
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 %                                                                             %
381 %                                                                             %
382 %                                                                             %
383 %     A p p e n d I m a g e s                                                 %
384 %                                                                             %
385 %                                                                             %
386 %                                                                             %
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 %
389 %  AppendImages() takes all images from the current image pointer to the end
390 %  of the image list and appends them to each other top-to-bottom if the
391 %  stack parameter is true, otherwise left-to-right.
392 %
393 %  The current gravity setting now effects how the image is justified in the
394 %  final image.
395 %
396 %  The format of the AppendImages method is:
397 %
398 %      Image *AppendImages(const Image *images,const MagickBooleanType stack,
399 %        ExceptionInfo *exception)
400 %
401 %  A description of each parameter follows:
402 %
403 %    o images: the image sequence.
404 %
405 %    o stack: A value other than 0 stacks the images top-to-bottom.
406 %
407 %    o exception: return any errors or warnings in this structure.
408 %
409 */
410 MagickExport Image *AppendImages(const Image *images,
411   const MagickBooleanType stack,ExceptionInfo *exception)
412 {
413 #define AppendImageTag  "Append/Image"
414
415   CacheView
416     *append_view,
417     *image_view;
418
419   const Image
420     *image;
421
422   Image
423     *append_image;
424
425   MagickBooleanType
426     matte,
427     proceed,
428     status;
429
430   MagickOffsetType
431     n;
432
433   RectangleInfo
434     geometry;
435
436   register const Image
437     *next;
438
439   size_t
440     height,
441     number_images,
442     width;
443
444   ssize_t
445     x_offset,
446     y,
447     y_offset;
448
449   /*
450     Compute maximum area of appended area.
451   */
452   assert(images != (Image *) NULL);
453   assert(images->signature == MagickSignature);
454   if (images->debug != MagickFalse)
455     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
456   assert(exception != (ExceptionInfo *) NULL);
457   assert(exception->signature == MagickSignature);
458   image=images;
459   matte=image->matte;
460   number_images=1;
461   width=image->columns;
462   height=image->rows;
463   next=GetNextImageInList(image);
464   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
465   {
466     if (next->matte != MagickFalse)
467       matte=MagickTrue;
468     number_images++;
469     if (stack != MagickFalse)
470       {
471         if (next->columns > width)
472           width=next->columns;
473         height+=next->rows;
474         continue;
475       }
476     width+=next->columns;
477     if (next->rows > height)
478       height=next->rows;
479   }
480   /*
481     Append images.
482   */
483   append_image=CloneImage(image,width,height,MagickTrue,exception);
484   if (append_image == (Image *) NULL)
485     return((Image *) NULL);
486   if (SetImageStorageClass(append_image,DirectClass) == MagickFalse)
487     {
488       InheritException(exception,&append_image->exception);
489       append_image=DestroyImage(append_image);
490       return((Image *) NULL);
491     }
492   append_image->matte=matte;
493   (void) SetImageBackgroundColor(append_image);
494   status=MagickTrue;
495   x_offset=0;
496   y_offset=0;
497   append_view=AcquireCacheView(append_image);
498   for (n=0; n < (MagickOffsetType) number_images; n++)
499   {
500     SetGeometry(append_image,&geometry);
501     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
502     if (stack != MagickFalse)
503       x_offset-=geometry.x;
504     else
505       y_offset-=geometry.y;
506     image_view=AcquireCacheView(image);
507 #if defined(MAGICKCORE_OPENMP_SUPPORT)
508     #pragma omp parallel for schedule(dynamic,4) shared(status) omp_throttle(1)
509 #endif
510     for (y=0; y < (ssize_t) image->rows; y++)
511     {
512       MagickBooleanType
513         sync;
514
515       register const IndexPacket
516         *restrict indexes;
517
518       register const PixelPacket
519         *restrict p;
520
521       register IndexPacket
522         *restrict append_indexes;
523
524       register PixelPacket
525         *restrict q;
526
527       register ssize_t
528         x;
529
530       if (status == MagickFalse)
531         continue;
532       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
533       q=QueueCacheViewAuthenticPixels(append_view,x_offset,y+y_offset,
534         image->columns,1,exception);
535       if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
536         {
537           status=MagickFalse;
538           continue;
539         }
540       indexes=GetCacheViewVirtualIndexQueue(image_view);
541       append_indexes=GetCacheViewAuthenticIndexQueue(append_view);
542       for (x=0; x < (ssize_t) image->columns; x++)
543       {
544         SetRedPixelComponent(q,GetRedPixelComponent(p));
545         SetGreenPixelComponent(q,GetGreenPixelComponent(p));
546         SetBluePixelComponent(q,GetBluePixelComponent(p));
547         SetOpacityPixelComponent(q,OpaqueOpacity);
548         if (image->matte != MagickFalse)
549           SetOpacityPixelComponent(q,GetOpacityPixelComponent(p));
550         if ((image->colorspace == CMYKColorspace) &&
551             (append_image->colorspace == CMYKColorspace))
552           SetIndexPixelComponent(append_indexes+x,GetIndexPixelComponent(
553             indexes+x));
554         p++;
555         q++;
556       }
557       sync=SyncCacheViewAuthenticPixels(append_view,exception);
558       if (sync == MagickFalse)
559         status=MagickFalse;
560     }
561     image_view=DestroyCacheView(image_view);
562     proceed=SetImageProgress(image,AppendImageTag,n,number_images);
563     if (proceed == MagickFalse)
564       break;
565     if (stack == MagickFalse)
566       {
567         x_offset+=(ssize_t) image->columns;
568         y_offset=0;
569       }
570     else
571       {
572         x_offset=0;
573         y_offset+=(ssize_t) image->rows;
574       }
575     image=GetNextImageInList(image);
576   }
577   append_view=DestroyCacheView(append_view);
578   if (status == MagickFalse)
579     append_image=DestroyImage(append_image);
580   return(append_image);
581 }
582 \f
583 /*
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 %                                                                             %
586 %                                                                             %
587 %                                                                             %
588 %   C a t c h I m a g e E x c e p t i o n                                     %
589 %                                                                             %
590 %                                                                             %
591 %                                                                             %
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 %
594 %  CatchImageException() returns if no exceptions are found in the image
595 %  sequence, otherwise it determines the most severe exception and reports
596 %  it as a warning or error depending on the severity.
597 %
598 %  The format of the CatchImageException method is:
599 %
600 %      ExceptionType CatchImageException(Image *image)
601 %
602 %  A description of each parameter follows:
603 %
604 %    o image: An image sequence.
605 %
606 */
607 MagickExport ExceptionType CatchImageException(Image *image)
608 {
609   ExceptionInfo
610     *exception;
611
612   ExceptionType
613     severity;
614
615   assert(image != (const Image *) NULL);
616   assert(image->signature == MagickSignature);
617   if (image->debug != MagickFalse)
618     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
619   exception=AcquireExceptionInfo();
620   GetImageException(image,exception);
621   CatchException(exception);
622   severity=exception->severity;
623   exception=DestroyExceptionInfo(exception);
624   return(severity);
625 }
626 \f
627 /*
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 %                                                                             %
630 %                                                                             %
631 %                                                                             %
632 %   C l i p I m a g e P a t h                                                 %
633 %                                                                             %
634 %                                                                             %
635 %                                                                             %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 %
638 %  ClipImagePath() sets the image clip mask based any clipping path information
639 %  if it exists.
640 %
641 %  The format of the ClipImagePath method is:
642 %
643 %      MagickBooleanType ClipImagePath(Image *image,const char *pathname,
644 %        const MagickBooleanType inside)
645 %
646 %  A description of each parameter follows:
647 %
648 %    o image: the image.
649 %
650 %    o pathname: name of clipping path resource. If name is preceded by #, use
651 %      clipping path numbered by name.
652 %
653 %    o inside: if non-zero, later operations take effect inside clipping path.
654 %      Otherwise later operations take effect outside clipping path.
655 %
656 */
657
658 MagickExport MagickBooleanType ClipImage(Image *image)
659 {
660   return(ClipImagePath(image,"#1",MagickTrue));
661 }
662
663 MagickExport MagickBooleanType ClipImagePath(Image *image,const char *pathname,
664   const MagickBooleanType inside)
665 {
666 #define ClipImagePathTag  "ClipPath/Image"
667
668   char
669     *property;
670
671   const char
672     *value;
673
674   Image
675     *clip_mask;
676
677   ImageInfo
678     *image_info;
679
680   assert(image != (const Image *) NULL);
681   assert(image->signature == MagickSignature);
682   if (image->debug != MagickFalse)
683     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
684   assert(pathname != NULL);
685   property=AcquireString(pathname);
686   (void) FormatLocaleString(property,MaxTextExtent,"8BIM:1999,2998:%s",
687     pathname);
688   value=GetImageProperty(image,property);
689   property=DestroyString(property);
690   if (value == (const char *) NULL)
691     {
692       ThrowFileException(&image->exception,OptionError,"NoClipPathDefined",
693         image->filename);
694       return(MagickFalse);
695     }
696   image_info=AcquireImageInfo();
697   (void) CopyMagickString(image_info->filename,image->filename,MaxTextExtent);
698   (void) ConcatenateMagickString(image_info->filename,pathname,MaxTextExtent);
699   clip_mask=BlobToImage(image_info,value,strlen(value),&image->exception);
700   image_info=DestroyImageInfo(image_info);
701   if (clip_mask == (Image *) NULL)
702     return(MagickFalse);
703   if (clip_mask->storage_class == PseudoClass)
704     {
705       (void) SyncImage(clip_mask);
706       if (SetImageStorageClass(clip_mask,DirectClass) == MagickFalse)
707         return(MagickFalse);
708     }
709   if (inside == MagickFalse)
710     (void) NegateImage(clip_mask,MagickFalse);
711   (void) FormatLocaleString(clip_mask->magick_filename,MaxTextExtent,
712     "8BIM:1999,2998:%s\nPS",pathname);
713   (void) SetImageClipMask(image,clip_mask);
714   clip_mask=DestroyImage(clip_mask);
715   return(MagickTrue);
716 }
717 \f
718 /*
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 %                                                                             %
721 %                                                                             %
722 %                                                                             %
723 %   C l o n e I m a g e                                                       %
724 %                                                                             %
725 %                                                                             %
726 %                                                                             %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 %
729 %  CloneImage() copies an image and returns the copy as a new image object.
730 %
731 %  If the specified columns and rows is 0, an exact copy of the image is
732 %  returned, otherwise the pixel data is undefined and must be initialized
733 %  with the QueueAuthenticPixels() and SyncAuthenticPixels() methods.  On
734 %  failure, a NULL image is returned and exception describes the reason for the
735 %  failure.
736 %
737 %  The format of the CloneImage method is:
738 %
739 %      Image *CloneImage(const Image *image,const size_t columns,
740 %        const size_t rows,const MagickBooleanType orphan,
741 %        ExceptionInfo *exception)
742 %
743 %  A description of each parameter follows:
744 %
745 %    o image: the image.
746 %
747 %    o columns: the number of columns in the cloned image.
748 %
749 %    o rows: the number of rows in the cloned image.
750 %
751 %    o detach:  With a value other than 0, the cloned image is detached from
752 %      its parent I/O stream.
753 %
754 %    o exception: return any errors or warnings in this structure.
755 %
756 */
757 MagickExport Image *CloneImage(const Image *image,const size_t columns,
758   const size_t rows,const MagickBooleanType detach,ExceptionInfo *exception)
759 {
760   Image
761     *clone_image;
762
763   MagickRealType
764     scale;
765
766   size_t
767     length;
768
769   /*
770     Clone the image.
771   */
772   assert(image != (const Image *) NULL);
773   assert(image->signature == MagickSignature);
774   if (image->debug != MagickFalse)
775     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
776   assert(exception != (ExceptionInfo *) NULL);
777   assert(exception->signature == MagickSignature);
778   clone_image=(Image *) AcquireMagickMemory(sizeof(*clone_image));
779   if (clone_image == (Image *) NULL)
780     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
781   (void) ResetMagickMemory(clone_image,0,sizeof(*clone_image));
782   clone_image->signature=MagickSignature;
783   clone_image->storage_class=image->storage_class;
784   clone_image->channels=image->channels;
785   clone_image->colorspace=image->colorspace;
786   clone_image->matte=image->matte;
787   clone_image->columns=image->columns;
788   clone_image->rows=image->rows;
789   clone_image->dither=image->dither;
790   if (image->colormap != (PixelPacket *) NULL)
791     {
792       /*
793         Allocate and copy the image colormap.
794       */
795       clone_image->colors=image->colors;
796       length=(size_t) image->colors;
797       clone_image->colormap=(PixelPacket *) AcquireQuantumMemory(length,
798         sizeof(*clone_image->colormap));
799       if (clone_image->colormap == (PixelPacket *) NULL)
800         ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
801       (void) CopyMagickMemory(clone_image->colormap,image->colormap,length*
802         sizeof(*clone_image->colormap));
803     }
804   (void) CloneImageProfiles(clone_image,image);
805   (void) CloneImageProperties(clone_image,image);
806   (void) CloneImageArtifacts(clone_image,image);
807   GetTimerInfo(&clone_image->timer);
808   GetExceptionInfo(&clone_image->exception);
809   InheritException(&clone_image->exception,&image->exception);
810   if (image->ascii85 != (void *) NULL)
811     Ascii85Initialize(clone_image);
812   clone_image->magick_columns=image->magick_columns;
813   clone_image->magick_rows=image->magick_rows;
814   clone_image->type=image->type;
815   (void) CopyMagickString(clone_image->magick_filename,image->magick_filename,
816     MaxTextExtent);
817   (void) CopyMagickString(clone_image->magick,image->magick,MaxTextExtent);
818   (void) CopyMagickString(clone_image->filename,image->filename,MaxTextExtent);
819   clone_image->progress_monitor=image->progress_monitor;
820   clone_image->client_data=image->client_data;
821   clone_image->reference_count=1;
822   clone_image->next=image->next;
823   clone_image->previous=image->previous;
824   clone_image->list=NewImageList();
825   clone_image->clip_mask=NewImageList();
826   clone_image->mask=NewImageList();
827   if (detach == MagickFalse)
828     clone_image->blob=ReferenceBlob(image->blob);
829   else
830     {
831       clone_image->next=NewImageList();
832       clone_image->previous=NewImageList();
833       clone_image->blob=CloneBlobInfo((BlobInfo *) NULL);
834     }
835   clone_image->ping=image->ping;
836   clone_image->debug=IsEventLogging();
837   clone_image->semaphore=AllocateSemaphoreInfo();
838   if ((columns == 0) && (rows == 0))
839     {
840       if (image->montage != (char *) NULL)
841         (void) CloneString(&clone_image->montage,image->montage);
842       if (image->directory != (char *) NULL)
843         (void) CloneString(&clone_image->directory,image->directory);
844       if (image->clip_mask != (Image *) NULL)
845         clone_image->clip_mask=CloneImage(image->clip_mask,0,0,MagickTrue,
846           exception);
847       if (image->mask != (Image *) NULL)
848         clone_image->mask=CloneImage(image->mask,0,0,MagickTrue,exception);
849       clone_image->cache=ReferencePixelCache(image->cache);
850       return(clone_image);
851     }
852   if ((columns == image->columns) && (rows == image->rows))
853     {
854       if (image->clip_mask != (Image *) NULL)
855         clone_image->clip_mask=CloneImage(image->clip_mask,0,0,MagickTrue,
856           exception);
857       if (image->mask != (Image *) NULL)
858         clone_image->mask=CloneImage(image->mask,0,0,MagickTrue,exception);
859     }
860   scale=(MagickRealType) columns/(MagickRealType) image->columns;
861   clone_image->page.width=(size_t) floor(scale*image->page.width+0.5);
862   clone_image->page.x=(ssize_t) ceil(scale*image->page.x-0.5);
863   clone_image->tile_offset.x=(ssize_t) ceil(scale*image->tile_offset.x-0.5);
864   scale=(MagickRealType) rows/(MagickRealType) image->rows;
865   clone_image->page.height=(size_t) floor(scale*image->page.height+0.5);
866   clone_image->page.y=(ssize_t) ceil(scale*image->page.y-0.5);
867   clone_image->tile_offset.y=(ssize_t) ceil(scale*image->tile_offset.y-0.5);
868   clone_image->columns=columns;
869   clone_image->rows=rows;
870   clone_image->cache=ClonePixelCache(image->cache);
871   return(clone_image);
872 }
873 \f
874 /*
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 %                                                                             %
877 %                                                                             %
878 %                                                                             %
879 %   C l o n e I m a g e I n f o                                               %
880 %                                                                             %
881 %                                                                             %
882 %                                                                             %
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 %
885 %  CloneImageInfo() makes a copy of the given image info structure.  If
886 %  NULL is specified, a new image info structure is created initialized to
887 %  default values.
888 %
889 %  The format of the CloneImageInfo method is:
890 %
891 %      ImageInfo *CloneImageInfo(const ImageInfo *image_info)
892 %
893 %  A description of each parameter follows:
894 %
895 %    o image_info: the image info.
896 %
897 */
898 MagickExport ImageInfo *CloneImageInfo(const ImageInfo *image_info)
899 {
900   ImageInfo
901     *clone_info;
902
903   clone_info=AcquireImageInfo();
904   if (image_info == (ImageInfo *) NULL)
905     return(clone_info);
906   clone_info->compression=image_info->compression;
907   clone_info->temporary=image_info->temporary;
908   clone_info->adjoin=image_info->adjoin;
909   clone_info->antialias=image_info->antialias;
910   clone_info->scene=image_info->scene;
911   clone_info->number_scenes=image_info->number_scenes;
912   clone_info->depth=image_info->depth;
913   if (image_info->size != (char *) NULL)
914     (void) CloneString(&clone_info->size,image_info->size);
915   if (image_info->extract != (char *) NULL)
916     (void) CloneString(&clone_info->extract,image_info->extract);
917   if (image_info->scenes != (char *) NULL)
918     (void) CloneString(&clone_info->scenes,image_info->scenes);
919   if (image_info->page != (char *) NULL)
920     (void) CloneString(&clone_info->page,image_info->page);
921   clone_info->interlace=image_info->interlace;
922   clone_info->endian=image_info->endian;
923   clone_info->units=image_info->units;
924   clone_info->quality=image_info->quality;
925   if (image_info->sampling_factor != (char *) NULL)
926     (void) CloneString(&clone_info->sampling_factor,
927       image_info->sampling_factor);
928   if (image_info->server_name != (char *) NULL)
929     (void) CloneString(&clone_info->server_name,image_info->server_name);
930   if (image_info->font != (char *) NULL)
931     (void) CloneString(&clone_info->font,image_info->font);
932   if (image_info->texture != (char *) NULL)
933     (void) CloneString(&clone_info->texture,image_info->texture);
934   if (image_info->density != (char *) NULL)
935     (void) CloneString(&clone_info->density,image_info->density);
936   clone_info->pointsize=image_info->pointsize;
937   clone_info->fuzz=image_info->fuzz;
938   clone_info->pen=image_info->pen;
939   clone_info->background_color=image_info->background_color;
940   clone_info->border_color=image_info->border_color;
941   clone_info->matte_color=image_info->matte_color;
942   clone_info->transparent_color=image_info->transparent_color;
943   clone_info->dither=image_info->dither;
944   clone_info->monochrome=image_info->monochrome;
945   clone_info->colors=image_info->colors;
946   clone_info->colorspace=image_info->colorspace;
947   clone_info->type=image_info->type;
948   clone_info->orientation=image_info->orientation;
949   clone_info->preview_type=image_info->preview_type;
950   clone_info->group=image_info->group;
951   clone_info->ping=image_info->ping;
952   clone_info->verbose=image_info->verbose;
953   if (image_info->view != (char *) NULL)
954     (void) CloneString(&clone_info->view,image_info->view);
955   if (image_info->authenticate != (char *) NULL)
956     (void) CloneString(&clone_info->authenticate,image_info->authenticate);
957   (void) CloneImageOptions(clone_info,image_info);
958   clone_info->progress_monitor=image_info->progress_monitor;
959   clone_info->client_data=image_info->client_data;
960   clone_info->cache=image_info->cache;
961   if (image_info->cache != (void *) NULL)
962     clone_info->cache=ReferencePixelCache(image_info->cache);
963   if (image_info->profile != (void *) NULL)
964     clone_info->profile=(void *) CloneStringInfo((StringInfo *)
965       image_info->profile);
966   SetImageInfoFile(clone_info,image_info->file);
967   SetImageInfoBlob(clone_info,image_info->blob,image_info->length);
968   clone_info->stream=image_info->stream;
969   clone_info->virtual_pixel_method=image_info->virtual_pixel_method;
970   (void) CopyMagickString(clone_info->magick,image_info->magick,MaxTextExtent);
971   (void) CopyMagickString(clone_info->unique,image_info->unique,MaxTextExtent);
972   (void) CopyMagickString(clone_info->zero,image_info->zero,MaxTextExtent);
973   (void) CopyMagickString(clone_info->filename,image_info->filename,
974     MaxTextExtent);
975   clone_info->subimage=image_info->scene;  /* deprecated */
976   clone_info->subrange=image_info->number_scenes;  /* deprecated */
977   clone_info->channel=image_info->channel;
978   clone_info->debug=IsEventLogging();
979   clone_info->signature=image_info->signature;
980   return(clone_info);
981 }
982 \f
983 /*
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 %                                                                             %
986 %                                                                             %
987 %                                                                             %
988 %     C o m b i n e I m a g e s                                               %
989 %                                                                             %
990 %                                                                             %
991 %                                                                             %
992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 %
994 %  CombineImages() combines one or more images into a single image.  The
995 %  grayscale value of the pixels of each image in the sequence is assigned in
996 %  order to the specified channels of the combined image.   The typical
997 %  ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
998 %
999 %  The format of the CombineImages method is:
1000 %
1001 %      Image *CombineImages(const Image *image,const ChannelType channel,
1002 %        ExceptionInfo *exception)
1003 %
1004 %  A description of each parameter follows:
1005 %
1006 %    o image: the image.
1007 %
1008 %    o exception: return any errors or warnings in this structure.
1009 %
1010 */
1011 MagickExport Image *CombineImages(const Image *image,const ChannelType channel,
1012   ExceptionInfo *exception)
1013 {
1014 #define CombineImageTag  "Combine/Image"
1015
1016   CacheView
1017     *combine_view;
1018
1019   const Image
1020     *next;
1021
1022   Image
1023     *combine_image;
1024
1025   MagickBooleanType
1026     status;
1027
1028   MagickOffsetType
1029     progress;
1030
1031   ssize_t
1032     y;
1033
1034   /*
1035     Ensure the image are the same size.
1036   */
1037   assert(image != (const Image *) NULL);
1038   assert(image->signature == MagickSignature);
1039   if (image->debug != MagickFalse)
1040     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1041   assert(exception != (ExceptionInfo *) NULL);
1042   assert(exception->signature == MagickSignature);
1043   for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1044   {
1045     if ((next->columns != image->columns) || (next->rows != image->rows))
1046       ThrowImageException(OptionError,"ImagesAreNotTheSameSize");
1047   }
1048   combine_image=CloneImage(image,0,0,MagickTrue,exception);
1049   if (combine_image == (Image *) NULL)
1050     return((Image *) NULL);
1051   if (SetImageStorageClass(combine_image,DirectClass) == MagickFalse)
1052     {
1053       InheritException(exception,&combine_image->exception);
1054       combine_image=DestroyImage(combine_image);
1055       return((Image *) NULL);
1056     }
1057   if ((channel & OpacityChannel) != 0)
1058     combine_image->matte=MagickTrue;
1059   (void) SetImageBackgroundColor(combine_image);
1060   /*
1061     Combine images.
1062   */
1063   status=MagickTrue;
1064   progress=0;
1065   combine_view=AcquireCacheView(combine_image);
1066   for (y=0; y < (ssize_t) combine_image->rows; y++)
1067   {
1068     CacheView
1069       *image_view;
1070
1071     const Image
1072       *next;
1073
1074     PixelPacket
1075       *pixels;
1076
1077     register const PixelPacket
1078       *restrict p;
1079
1080     register PixelPacket
1081       *restrict q;
1082
1083     register ssize_t
1084       x;
1085
1086     if (status == MagickFalse)
1087       continue;
1088     pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
1089       1,exception);
1090     if (pixels == (PixelPacket *) NULL)
1091       {
1092         status=MagickFalse;
1093         continue;
1094       }
1095     next=image;
1096     if (((channel & RedChannel) != 0) && (next != (Image *) NULL))
1097       {
1098         image_view=AcquireCacheView(next);
1099         p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
1100         if (p == (const PixelPacket *) NULL)
1101           continue;
1102         q=pixels;
1103         for (x=0; x < (ssize_t) combine_image->columns; x++)
1104         {
1105           SetRedPixelComponent(q,PixelIntensityToQuantum(p));
1106           p++;
1107           q++;
1108         }
1109         image_view=DestroyCacheView(image_view);
1110         next=GetNextImageInList(next);
1111       }
1112     if (((channel & GreenChannel) != 0) && (next != (Image *) NULL))
1113       {
1114         image_view=AcquireCacheView(next);
1115         p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
1116         if (p == (const PixelPacket *) NULL)
1117           continue;
1118         q=pixels;
1119         for (x=0; x < (ssize_t) combine_image->columns; x++)
1120         {
1121           SetGreenPixelComponent(q,PixelIntensityToQuantum(p));
1122           p++;
1123           q++;
1124         }
1125         image_view=DestroyCacheView(image_view);
1126         next=GetNextImageInList(next);
1127       }
1128     if (((channel & BlueChannel) != 0) && (next != (Image *) NULL))
1129       {
1130         image_view=AcquireCacheView(next);
1131         p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
1132         if (p == (const PixelPacket *) NULL)
1133           continue;
1134         q=pixels;
1135         for (x=0; x < (ssize_t) combine_image->columns; x++)
1136         {
1137           SetBluePixelComponent(q,PixelIntensityToQuantum(p));
1138           p++;
1139           q++;
1140         }
1141         image_view=DestroyCacheView(image_view);
1142         next=GetNextImageInList(next);
1143       }
1144     if (((channel & OpacityChannel) != 0) && (next != (Image *) NULL))
1145       {
1146         image_view=AcquireCacheView(next);
1147         p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
1148         if (p == (const PixelPacket *) NULL)
1149           continue;
1150         q=pixels;
1151         for (x=0; x < (ssize_t) combine_image->columns; x++)
1152         {
1153           SetOpacityPixelComponent(q,PixelIntensityToQuantum(p));
1154           p++;
1155           q++;
1156         }
1157         image_view=DestroyCacheView(image_view);
1158         next=GetNextImageInList(next);
1159       }
1160     if (((channel & IndexChannel) != 0) &&
1161         (image->colorspace == CMYKColorspace) && (next != (Image *) NULL))
1162       {
1163         IndexPacket
1164           *indexes;
1165
1166         image_view=AcquireCacheView(next);
1167         p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
1168         if (p == (const PixelPacket *) NULL)
1169           continue;
1170         indexes=GetCacheViewAuthenticIndexQueue(combine_view);
1171         for (x=0; x < (ssize_t) combine_image->columns; x++)
1172         {
1173           SetIndexPixelComponent(indexes+x,PixelIntensityToQuantum(p));
1174           p++;
1175         }
1176         image_view=DestroyCacheView(image_view);
1177         next=GetNextImageInList(next);
1178       }
1179     if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
1180       status=MagickFalse;
1181     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1182       {
1183         MagickBooleanType
1184           proceed;
1185
1186         proceed=SetImageProgress(image,CombineImageTag,progress++,
1187           combine_image->rows);
1188         if (proceed == MagickFalse)
1189           status=MagickFalse;
1190       }
1191   }
1192   combine_view=DestroyCacheView(combine_view);
1193   if (status == MagickFalse)
1194     combine_image=DestroyImage(combine_image);
1195   return(combine_image);
1196 }
1197 \f
1198 /*
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 %                                                                             %
1201 %                                                                             %
1202 %                                                                             %
1203 %   D e s t r o y I m a g e                                                   %
1204 %                                                                             %
1205 %                                                                             %
1206 %                                                                             %
1207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208 %
1209 %  DestroyImage() dereferences an image, deallocating memory associated with
1210 %  the image if the reference count becomes zero.
1211 %
1212 %  The format of the DestroyImage method is:
1213 %
1214 %      Image *DestroyImage(Image *image)
1215 %
1216 %  A description of each parameter follows:
1217 %
1218 %    o image: the image.
1219 %
1220 */
1221 MagickExport Image *DestroyImage(Image *image)
1222 {
1223   MagickBooleanType
1224     destroy;
1225
1226   /*
1227     Dereference image.
1228   */
1229   assert(image != (Image *) NULL);
1230   assert(image->signature == MagickSignature);
1231   if (image->debug != MagickFalse)
1232     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1233   destroy=MagickFalse;
1234   LockSemaphoreInfo(image->semaphore);
1235   image->reference_count--;
1236   if (image->reference_count == 0)
1237     destroy=MagickTrue;
1238   UnlockSemaphoreInfo(image->semaphore);
1239   if (destroy == MagickFalse)
1240     return((Image *) NULL);
1241   /*
1242     Destroy image.
1243   */
1244   DestroyImagePixels(image);
1245   if (image->clip_mask != (Image *) NULL)
1246     image->clip_mask=DestroyImage(image->clip_mask);
1247   if (image->mask != (Image *) NULL)
1248     image->mask=DestroyImage(image->mask);
1249   if (image->montage != (char *) NULL)
1250     image->montage=DestroyString(image->montage);
1251   if (image->directory != (char *) NULL)
1252     image->directory=DestroyString(image->directory);
1253   if (image->colormap != (PixelPacket *) NULL)
1254     image->colormap=(PixelPacket *) RelinquishMagickMemory(image->colormap);
1255   if (image->geometry != (char *) NULL)
1256     image->geometry=DestroyString(image->geometry);
1257   DestroyImageProfiles(image);
1258   DestroyImageProperties(image);
1259   DestroyImageArtifacts(image);
1260   if (image->ascii85 != (Ascii85Info*) NULL)
1261     image->ascii85=(Ascii85Info *) RelinquishMagickMemory(image->ascii85);
1262   DestroyBlob(image);
1263   (void) DestroyExceptionInfo(&image->exception);
1264   if (image->semaphore != (SemaphoreInfo *) NULL)
1265     DestroySemaphoreInfo(&image->semaphore);
1266   image->signature=(~MagickSignature);
1267   image=(Image *) RelinquishMagickMemory(image);
1268   return(image);
1269 }
1270 \f
1271 /*
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273 %                                                                             %
1274 %                                                                             %
1275 %                                                                             %
1276 %   D e s t r o y I m a g e I n f o                                           %
1277 %                                                                             %
1278 %                                                                             %
1279 %                                                                             %
1280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1281 %
1282 %  DestroyImageInfo() deallocates memory associated with an ImageInfo
1283 %  structure.
1284 %
1285 %  The format of the DestroyImageInfo method is:
1286 %
1287 %      ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1288 %
1289 %  A description of each parameter follows:
1290 %
1291 %    o image_info: the image info.
1292 %
1293 */
1294 MagickExport ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1295 {
1296   assert(image_info != (ImageInfo *) NULL);
1297   assert(image_info->signature == MagickSignature);
1298   if (image_info->debug != MagickFalse)
1299     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1300       image_info->filename);
1301   if (image_info->size != (char *) NULL)
1302     image_info->size=DestroyString(image_info->size);
1303   if (image_info->extract != (char *) NULL)
1304     image_info->extract=DestroyString(image_info->extract);
1305   if (image_info->scenes != (char *) NULL)
1306     image_info->scenes=DestroyString(image_info->scenes);
1307   if (image_info->page != (char *) NULL)
1308     image_info->page=DestroyString(image_info->page);
1309   if (image_info->sampling_factor != (char *) NULL)
1310     image_info->sampling_factor=DestroyString(
1311       image_info->sampling_factor);
1312   if (image_info->server_name != (char *) NULL)
1313     image_info->server_name=DestroyString(
1314       image_info->server_name);
1315   if (image_info->font != (char *) NULL)
1316     image_info->font=DestroyString(image_info->font);
1317   if (image_info->texture != (char *) NULL)
1318     image_info->texture=DestroyString(image_info->texture);
1319   if (image_info->density != (char *) NULL)
1320     image_info->density=DestroyString(image_info->density);
1321   if (image_info->view != (char *) NULL)
1322     image_info->view=DestroyString(image_info->view);
1323   if (image_info->authenticate != (char *) NULL)
1324     image_info->authenticate=DestroyString(
1325       image_info->authenticate);
1326   DestroyImageOptions(image_info);
1327   if (image_info->cache != (void *) NULL)
1328     image_info->cache=DestroyPixelCache(image_info->cache);
1329   if (image_info->profile != (StringInfo *) NULL)
1330     image_info->profile=(void *) DestroyStringInfo((StringInfo *)
1331       image_info->profile);
1332   image_info->signature=(~MagickSignature);
1333   image_info=(ImageInfo *) RelinquishMagickMemory(image_info);
1334   return(image_info);
1335 }
1336 \f
1337 /*
1338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1339 %                                                                             %
1340 %                                                                             %
1341 %                                                                             %
1342 +   D i s a s s o c i a t e I m a g e S t r e a m                             %
1343 %                                                                             %
1344 %                                                                             %
1345 %                                                                             %
1346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1347 %
1348 %  DisassociateImageStream() disassociates the image stream.
1349 %
1350 %  The format of the DisassociateImageStream method is:
1351 %
1352 %      MagickBooleanType DisassociateImageStream(const Image *image)
1353 %
1354 %  A description of each parameter follows:
1355 %
1356 %    o image: the image.
1357 %
1358 */
1359 MagickExport void DisassociateImageStream(Image *image)
1360 {
1361   assert(image != (const Image *) NULL);
1362   assert(image->signature == MagickSignature);
1363   if (image->debug != MagickFalse)
1364     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1365   (void) DetachBlob(image->blob);
1366 }
1367 \f
1368 /*
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 %                                                                             %
1371 %                                                                             %
1372 %                                                                             %
1373 %   G e t I m a g e A l p h a C h a n n e l                                   %
1374 %                                                                             %
1375 %                                                                             %
1376 %                                                                             %
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 %
1379 %  GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
1380 %  not activated.  That is, the image is RGB rather than RGBA or CMYK rather
1381 %  than CMYKA.
1382 %
1383 %  The format of the GetImageAlphaChannel method is:
1384 %
1385 %      MagickBooleanType GetImageAlphaChannel(const Image *image)
1386 %
1387 %  A description of each parameter follows:
1388 %
1389 %    o image: the image.
1390 %
1391 */
1392 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
1393 {
1394   assert(image != (const Image *) NULL);
1395   if (image->debug != MagickFalse)
1396     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1397   assert(image->signature == MagickSignature);
1398   return(image->matte);
1399 }
1400 \f
1401 /*
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403 %                                                                             %
1404 %                                                                             %
1405 %                                                                             %
1406 %   G e t I m a g e C l i p M a s k                                           %
1407 %                                                                             %
1408 %                                                                             %
1409 %                                                                             %
1410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 %
1412 %  GetImageClipMask() returns the clip path associated with the image.
1413 %
1414 %  The format of the GetImageClipMask method is:
1415 %
1416 %      Image *GetImageClipMask(const Image *image,ExceptionInfo *exception)
1417 %
1418 %  A description of each parameter follows:
1419 %
1420 %    o image: the image.
1421 %
1422 */
1423 MagickExport Image *GetImageClipMask(const Image *image,
1424   ExceptionInfo *exception)
1425 {
1426   assert(image != (const Image *) NULL);
1427   if (image->debug != MagickFalse)
1428     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1429   assert(image->signature == MagickSignature);
1430   if (image->clip_mask == (Image *) NULL)
1431     return((Image *) NULL);
1432   return(CloneImage(image->clip_mask,0,0,MagickTrue,exception));
1433 }
1434 \f
1435 /*
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 %                                                                             %
1438 %                                                                             %
1439 %                                                                             %
1440 %   G e t I m a g e E x c e p t i o n                                         %
1441 %                                                                             %
1442 %                                                                             %
1443 %                                                                             %
1444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445 %
1446 %  GetImageException() traverses an image sequence and returns any
1447 %  error more severe than noted by the exception parameter.
1448 %
1449 %  The format of the GetImageException method is:
1450 %
1451 %      void GetImageException(Image *image,ExceptionInfo *exception)
1452 %
1453 %  A description of each parameter follows:
1454 %
1455 %    o image: Specifies a pointer to a list of one or more images.
1456 %
1457 %    o exception: return the highest severity exception.
1458 %
1459 */
1460 MagickExport void GetImageException(Image *image,ExceptionInfo *exception)
1461 {
1462   register Image
1463     *next;
1464
1465   assert(image != (Image *) NULL);
1466   assert(image->signature == MagickSignature);
1467   if (image->debug != MagickFalse)
1468     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1469   assert(exception != (ExceptionInfo *) NULL);
1470   assert(exception->signature == MagickSignature);
1471   for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
1472   {
1473     if (next->exception.severity == UndefinedException)
1474       continue;
1475     if (next->exception.severity > exception->severity)
1476       InheritException(exception,&next->exception);
1477     next->exception.severity=UndefinedException;
1478   }
1479 }
1480 \f
1481 /*
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 %                                                                             %
1484 %                                                                             %
1485 %                                                                             %
1486 %   G e t I m a g e I n f o                                                   %
1487 %                                                                             %
1488 %                                                                             %
1489 %                                                                             %
1490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 %
1492 %  GetImageInfo() initializes image_info to default values.
1493 %
1494 %  The format of the GetImageInfo method is:
1495 %
1496 %      void GetImageInfo(ImageInfo *image_info)
1497 %
1498 %  A description of each parameter follows:
1499 %
1500 %    o image_info: the image info.
1501 %
1502 */
1503 MagickExport void GetImageInfo(ImageInfo *image_info)
1504 {
1505   const char
1506     *synchronize;
1507
1508   ExceptionInfo
1509     *exception;
1510
1511   /*
1512     File and image dimension members.
1513   */
1514   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1515   assert(image_info != (ImageInfo *) NULL);
1516   (void) ResetMagickMemory(image_info,0,sizeof(*image_info));
1517   image_info->adjoin=MagickTrue;
1518   image_info->interlace=NoInterlace;
1519   image_info->channel=DefaultChannels;
1520   image_info->quality=UndefinedCompressionQuality;
1521   image_info->antialias=MagickTrue;
1522   image_info->dither=MagickTrue;
1523   synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
1524   if (synchronize != (const char *) NULL)
1525     image_info->synchronize=IsMagickTrue(synchronize);
1526   exception=AcquireExceptionInfo();
1527   (void) QueryColorDatabase(BackgroundColor,&image_info->background_color,
1528     exception);
1529   (void) QueryColorDatabase(BorderColor,&image_info->border_color,exception);
1530   (void) QueryColorDatabase(MatteColor,&image_info->matte_color,exception);
1531   (void) QueryColorDatabase(TransparentColor,&image_info->transparent_color,
1532     exception);
1533   exception=DestroyExceptionInfo(exception);
1534   image_info->debug=IsEventLogging();
1535   image_info->signature=MagickSignature;
1536 }
1537 \f
1538 /*
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540 %                                                                             %
1541 %                                                                             %
1542 %                                                                             %
1543 %   G e t I m a g e I n f o F i l e                                           %
1544 %                                                                             %
1545 %                                                                             %
1546 %                                                                             %
1547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1548 %
1549 %  GetImageInfoFile() returns the image info file member.
1550 %
1551 %  The format of the GetImageInfoFile method is:
1552 %
1553 %      FILE *GetImageInfoFile(const ImageInfo *image_info)
1554 %
1555 %  A description of each parameter follows:
1556 %
1557 %    o image_info: the image info.
1558 %
1559 */
1560 MagickExport FILE *GetImageInfoFile(const ImageInfo *image_info)
1561 {
1562   return(image_info->file);
1563 }
1564 \f
1565 /*
1566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 %                                                                             %
1568 %                                                                             %
1569 %                                                                             %
1570 %   G e t I m a g e M a s k                                                   %
1571 %                                                                             %
1572 %                                                                             %
1573 %                                                                             %
1574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575 %
1576 %  GetImageMask() returns the mask associated with the image.
1577 %
1578 %  The format of the GetImageMask method is:
1579 %
1580 %      Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1581 %
1582 %  A description of each parameter follows:
1583 %
1584 %    o image: the image.
1585 %
1586 */
1587 MagickExport Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1588 {
1589   assert(image != (const Image *) NULL);
1590   if (image->debug != MagickFalse)
1591     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1592   assert(image->signature == MagickSignature);
1593   if (image->mask == (Image *) NULL)
1594     return((Image *) NULL);
1595   return(CloneImage(image->mask,0,0,MagickTrue,exception));
1596 }
1597 \f
1598 /*
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 %                                                                             %
1601 %                                                                             %
1602 %                                                                             %
1603 %   G e t I m a g e C h a n n e l s                                           %
1604 %                                                                             %
1605 %                                                                             %
1606 %                                                                             %
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 %
1609 %  GetImageChannels() returns the number of pixel channels associated with the
1610 %  specified image.
1611 %
1612 %  The format of the GetChannels method is:
1613 %
1614 %      size_t GetImageChannels(Image *image)
1615 %
1616 %  A description of each parameter follows:
1617 %
1618 %    o image: the image.
1619 %
1620 */
1621 MagickExport size_t GetImageChannels(Image *image)
1622 {
1623   assert(image != (Image *) NULL);
1624   assert(image->signature == MagickSignature);
1625   if (image->debug != MagickFalse)
1626     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1627   return(image->channels);
1628 }
1629 \f
1630 /*
1631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632 %                                                                             %
1633 %                                                                             %
1634 %                                                                             %
1635 +   G e t I m a g e R e f e r e n c e C o u n t                               %
1636 %                                                                             %
1637 %                                                                             %
1638 %                                                                             %
1639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1640 %
1641 %  GetImageReferenceCount() returns the image reference count.
1642 %
1643 %  The format of the GetReferenceCount method is:
1644 %
1645 %      ssize_t GetImageReferenceCount(Image *image)
1646 %
1647 %  A description of each parameter follows:
1648 %
1649 %    o image: the image.
1650 %
1651 */
1652 MagickExport ssize_t GetImageReferenceCount(Image *image)
1653 {
1654   ssize_t
1655     reference_count;
1656
1657   assert(image != (Image *) NULL);
1658   assert(image->signature == MagickSignature);
1659   if (image->debug != MagickFalse)
1660     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1661   LockSemaphoreInfo(image->semaphore);
1662   reference_count=image->reference_count;
1663   UnlockSemaphoreInfo(image->semaphore);
1664   return(reference_count);
1665 }
1666 \f
1667 /*
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1669 %                                                                             %
1670 %                                                                             %
1671 %                                                                             %
1672 %   G e t I m a g e V i r t u a l P i x e l M e t h o d                       %
1673 %                                                                             %
1674 %                                                                             %
1675 %                                                                             %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1677 %
1678 %  GetImageVirtualPixelMethod() gets the "virtual pixels" method for the
1679 %  image.  A virtual pixel is any pixel access that is outside the boundaries
1680 %  of the image cache.
1681 %
1682 %  The format of the GetImageVirtualPixelMethod() method is:
1683 %
1684 %      VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1685 %
1686 %  A description of each parameter follows:
1687 %
1688 %    o image: the image.
1689 %
1690 */
1691 MagickExport VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1692 {
1693   assert(image != (Image *) NULL);
1694   assert(image->signature == MagickSignature);
1695   if (image->debug != MagickFalse)
1696     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1697   return(GetPixelCacheVirtualMethod(image));
1698 }
1699 \f
1700 /*
1701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 %                                                                             %
1703 %                                                                             %
1704 %                                                                             %
1705 %  I n t e r p r e t I m a g e F i l e n a m e                                %
1706 %                                                                             %
1707 %                                                                             %
1708 %                                                                             %
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 %
1711 %  InterpretImageFilename() interprets embedded characters in an image filename.
1712 %  The filename length is returned.
1713 %
1714 %  The format of the InterpretImageFilename method is:
1715 %
1716 %      size_t InterpretImageFilename(const ImageInfo *image_info,
1717 %        Image *image,const char *format,int value,char *filename)
1718 %
1719 %  A description of each parameter follows.
1720 %
1721 %    o image_info: the image info..
1722 %
1723 %    o image: the image.
1724 %
1725 %    o format:  A filename describing the format to use to write the numeric
1726 %      argument. Only the first numeric format identifier is replaced.
1727 %
1728 %    o value:  Numeric value to substitute into format filename.
1729 %
1730 %    o filename:  return the formatted filename in this character buffer.
1731 %
1732 */
1733 MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1734   Image *image,const char *format,int value,char *filename)
1735 {
1736   char
1737     *q;
1738
1739   int
1740     c;
1741
1742   MagickBooleanType
1743     canonical;
1744
1745   register const char
1746     *p;
1747
1748   canonical=MagickFalse;
1749   (void) CopyMagickString(filename,format,MaxTextExtent);
1750   for (p=strchr(format,'%'); p != (char *) NULL; p=strchr(p+1,'%'))
1751   {
1752     q=(char *) p+1;
1753     if (*q == '%')
1754       {
1755         p=q+1;
1756         continue;
1757       }
1758     if (*q == '0')
1759       {
1760         ssize_t
1761           value;
1762
1763         value=(ssize_t) strtol(q,&q,10);
1764         (void) value;
1765       }
1766     switch (*q)
1767     {
1768       case 'd':
1769       case 'o':
1770       case 'x':
1771       {
1772         q++;
1773         c=(*q);
1774         *q='\0';
1775         (void) FormatLocaleString(filename+(p-format),(size_t) (MaxTextExtent-
1776           (p-format)),p,value);
1777         *q=c;
1778         (void) ConcatenateMagickString(filename,q,MaxTextExtent);
1779         canonical=MagickTrue;
1780         if (*(q-1) != '%')
1781           break;
1782         p++;
1783         break;
1784       }
1785       case '[':
1786       {
1787         char
1788           pattern[MaxTextExtent];
1789
1790         const char
1791           *value;
1792
1793         register char
1794           *r;
1795
1796         register ssize_t
1797           i;
1798
1799         ssize_t
1800           depth;
1801
1802         /*
1803           Image option.
1804         */
1805         if (strchr(p,']') == (char *) NULL)
1806           break;
1807         depth=1;
1808         r=q+1;
1809         for (i=0; (i < (MaxTextExtent-1L)) && (*r != '\0'); i++)
1810         {
1811           if (*r == '[')
1812             depth++;
1813           if (*r == ']')
1814             depth--;
1815           if (depth <= 0)
1816             break;
1817           pattern[i]=(*r++);
1818         }
1819         pattern[i]='\0';
1820         if (LocaleNCompare(pattern,"filename:",9) != 0)
1821           break;
1822         value=(const char *) NULL;
1823         if ((image_info != (const ImageInfo *) NULL) &&
1824             (image != (const Image *) NULL))
1825           value=GetMagickProperty(image_info,image,pattern);
1826         else
1827           if (image != (Image *) NULL)
1828             value=GetImageProperty(image,pattern);
1829           else
1830             if (image_info != (ImageInfo *) NULL)
1831               value=GetImageOption(image_info,pattern);
1832         if (value == (const char *) NULL)
1833           break;
1834         q--;
1835         c=(*q);
1836         *q='\0';
1837         (void) CopyMagickString(filename+(p-format),value,(size_t)
1838           (MaxTextExtent-(p-format)));
1839         *q=c;
1840         (void) ConcatenateMagickString(filename,r+1,MaxTextExtent);
1841         canonical=MagickTrue;
1842         if (*(q-1) != '%')
1843           break;
1844         p++;
1845         break;
1846       }
1847       default:
1848         break;
1849     }
1850   }
1851   for (q=filename; *q != '\0'; q++)
1852     if ((*q == '%') && (*(q+1) == '%'))
1853       {
1854         (void) CopyMagickString(q,q+1,(size_t) (MaxTextExtent-(q-filename)));
1855         canonical=MagickTrue;
1856       }
1857   if (canonical == MagickFalse)
1858     (void) CopyMagickString(filename,format,MaxTextExtent);
1859   return(strlen(filename));
1860 }
1861 \f
1862 /*
1863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864 %                                                                             %
1865 %                                                                             %
1866 %                                                                             %
1867 %   I s H i g h D y n a m i c R a n g e I m a g e                             %
1868 %                                                                             %
1869 %                                                                             %
1870 %                                                                             %
1871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872 %
1873 %  IsHighDynamicRangeImage() returns MagickTrue if any pixel component is
1874 %  non-integer or exceeds the bounds of the quantum depth (e.g. for Q16
1875 %  0..65535.
1876 %
1877 %  The format of the IsHighDynamicRangeImage method is:
1878 %
1879 %      MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1880 %        ExceptionInfo *exception)
1881 %
1882 %  A description of each parameter follows:
1883 %
1884 %    o image: the image.
1885 %
1886 %    o exception: return any errors or warnings in this structure.
1887 %
1888 */
1889 MagickExport MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1890   ExceptionInfo *exception)
1891 {
1892 #if !defined(MAGICKCORE_HDRI_SUPPORT)
1893   (void) image;
1894   (void) exception;
1895   return(MagickFalse);
1896 #else
1897   CacheView
1898     *image_view;
1899
1900   MagickBooleanType
1901     status;
1902
1903   MagickPixelPacket
1904     zero;
1905
1906   ssize_t
1907     y;
1908
1909   assert(image != (Image *) NULL);
1910   assert(image->signature == MagickSignature);
1911   if (image->debug != MagickFalse)
1912     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1913   status=MagickTrue;
1914   GetMagickPixelPacket(image,&zero);
1915   image_view=AcquireCacheView(image);
1916 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1917   #pragma omp parallel for schedule(dynamic,4) shared(status)
1918 #endif
1919   for (y=0; y < (ssize_t) image->rows; y++)
1920   {
1921     MagickPixelPacket
1922       pixel;
1923
1924     register const IndexPacket
1925       *indexes;
1926
1927     register const PixelPacket
1928       *p;
1929
1930     register ssize_t
1931       x;
1932
1933     if (status == MagickFalse)
1934       continue;
1935     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1936     if (p == (const PixelPacket *) NULL)
1937       {
1938         status=MagickFalse;
1939         continue;
1940       }
1941     indexes=GetCacheViewVirtualIndexQueue(image_view);
1942     pixel=zero;
1943     for (x=0; x < (ssize_t) image->columns; x++)
1944     {
1945       SetMagickPixelPacket(image,p,indexes+x,&pixel);
1946       if ((pixel.red < 0.0) || (pixel.red > QuantumRange) ||
1947           (pixel.red != (QuantumAny) pixel.red))
1948         break;
1949       if ((pixel.green < 0.0) || (pixel.green > QuantumRange) ||
1950           (pixel.green != (QuantumAny) pixel.green))
1951         break;
1952       if ((pixel.blue < 0.0) || (pixel.blue > QuantumRange) ||
1953           (pixel.blue != (QuantumAny) pixel.blue))
1954         break;
1955       if (pixel.matte != MagickFalse)
1956         {
1957           if ((pixel.opacity < 0.0) || (pixel.opacity > QuantumRange) ||
1958               (pixel.opacity != (QuantumAny) pixel.opacity))
1959             break;
1960         }
1961       if (pixel.colorspace == CMYKColorspace)
1962         {
1963           if ((pixel.index < 0.0) || (pixel.index > QuantumRange) ||
1964               (pixel.index != (QuantumAny) pixel.index))
1965             break;
1966         }
1967       p++;
1968     }
1969     if (x < (ssize_t) image->columns)
1970       status=MagickFalse;
1971   }
1972   image_view=DestroyCacheView(image_view);
1973   return(status != MagickFalse ? MagickFalse : MagickTrue);
1974 #endif
1975 }
1976 \f
1977 /*
1978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1979 %                                                                             %
1980 %                                                                             %
1981 %                                                                             %
1982 %     I s I m a g e O b j e c t                                               %
1983 %                                                                             %
1984 %                                                                             %
1985 %                                                                             %
1986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1987 %
1988 %  IsImageObject() returns MagickTrue if the image sequence contains a valid
1989 %  set of image objects.
1990 %
1991 %  The format of the IsImageObject method is:
1992 %
1993 %      MagickBooleanType IsImageObject(const Image *image)
1994 %
1995 %  A description of each parameter follows:
1996 %
1997 %    o image: the image.
1998 %
1999 */
2000 MagickExport MagickBooleanType IsImageObject(const Image *image)
2001 {
2002   register const Image
2003     *p;
2004
2005   assert(image != (Image *) NULL);
2006   if (image->debug != MagickFalse)
2007     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2008   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
2009     if (p->signature != MagickSignature)
2010       return(MagickFalse);
2011   return(MagickTrue);
2012 }
2013 \f
2014 /*
2015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2016 %                                                                             %
2017 %                                                                             %
2018 %                                                                             %
2019 %     I s T a i n t I m a g e                                                 %
2020 %                                                                             %
2021 %                                                                             %
2022 %                                                                             %
2023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2024 %
2025 %  IsTaintImage() returns MagickTrue any pixel in the image has been altered
2026 %  since it was first constituted.
2027 %
2028 %  The format of the IsTaintImage method is:
2029 %
2030 %      MagickBooleanType IsTaintImage(const Image *image)
2031 %
2032 %  A description of each parameter follows:
2033 %
2034 %    o image: the image.
2035 %
2036 */
2037 MagickExport MagickBooleanType IsTaintImage(const Image *image)
2038 {
2039   char
2040     magick[MaxTextExtent],
2041     filename[MaxTextExtent];
2042
2043   register const Image
2044     *p;
2045
2046   assert(image != (Image *) NULL);
2047   if (image->debug != MagickFalse)
2048     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2049   assert(image->signature == MagickSignature);
2050   (void) CopyMagickString(magick,image->magick,MaxTextExtent);
2051   (void) CopyMagickString(filename,image->filename,MaxTextExtent);
2052   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
2053   {
2054     if (p->taint != MagickFalse)
2055       return(MagickTrue);
2056     if (LocaleCompare(p->magick,magick) != 0)
2057       return(MagickTrue);
2058     if (LocaleCompare(p->filename,filename) != 0)
2059       return(MagickTrue);
2060   }
2061   return(MagickFalse);
2062 }
2063 \f
2064 /*
2065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2066 %                                                                             %
2067 %                                                                             %
2068 %                                                                             %
2069 %   M o d i f y I m a g e                                                     %
2070 %                                                                             %
2071 %                                                                             %
2072 %                                                                             %
2073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2074 %
2075 %  ModifyImage() ensures that there is only a single reference to the image
2076 %  to be modified, updating the provided image pointer to point to a clone of
2077 %  the original image if necessary.
2078 %
2079 %  The format of the ModifyImage method is:
2080 %
2081 %      MagickBooleanType ModifyImage(Image *image,ExceptionInfo *exception)
2082 %
2083 %  A description of each parameter follows:
2084 %
2085 %    o image: the image.
2086 %
2087 %    o exception: return any errors or warnings in this structure.
2088 %
2089 */
2090 MagickExport MagickBooleanType ModifyImage(Image **image,
2091   ExceptionInfo *exception)
2092 {
2093   Image
2094     *clone_image;
2095
2096   assert(image != (Image **) NULL);
2097   assert(*image != (Image *) NULL);
2098   assert((*image)->signature == MagickSignature);
2099   if ((*image)->debug != MagickFalse)
2100     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2101   if (GetImageReferenceCount(*image) <= 1)
2102     return(MagickTrue);
2103   clone_image=CloneImage(*image,0,0,MagickTrue,exception);
2104   LockSemaphoreInfo((*image)->semaphore);
2105   (*image)->reference_count--;
2106   UnlockSemaphoreInfo((*image)->semaphore);
2107   *image=clone_image;
2108   return(MagickTrue);
2109 }
2110 \f
2111 /*
2112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113 %                                                                             %
2114 %                                                                             %
2115 %                                                                             %
2116 %   N e w M a g i c k I m a g e                                               %
2117 %                                                                             %
2118 %                                                                             %
2119 %                                                                             %
2120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2121 %
2122 %  NewMagickImage() creates a blank image canvas of the specified size and
2123 %  background color.
2124 %
2125 %  The format of the NewMagickImage method is:
2126 %
2127 %      Image *NewMagickImage(const ImageInfo *image_info,
2128 %        const size_t width,const size_t height,
2129 %        const MagickPixelPacket *background)
2130 %
2131 %  A description of each parameter follows:
2132 %
2133 %    o image: the image.
2134 %
2135 %    o width: the image width.
2136 %
2137 %    o height: the image height.
2138 %
2139 %    o background: the image color.
2140 %
2141 */
2142 MagickExport Image *NewMagickImage(const ImageInfo *image_info,
2143   const size_t width,const size_t height,
2144   const MagickPixelPacket *background)
2145 {
2146   CacheView
2147     *image_view;
2148
2149   ExceptionInfo
2150     *exception;
2151
2152   Image
2153     *image;
2154
2155   ssize_t
2156     y;
2157
2158   MagickBooleanType
2159     status;
2160
2161   assert(image_info != (const ImageInfo *) NULL);
2162   if (image_info->debug != MagickFalse)
2163     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2164   assert(image_info->signature == MagickSignature);
2165   assert(background != (const MagickPixelPacket *) NULL);
2166   image=AcquireImage(image_info);
2167   image->columns=width;
2168   image->rows=height;
2169   image->colorspace=background->colorspace;
2170   image->matte=background->matte;
2171   image->fuzz=background->fuzz;
2172   image->depth=background->depth;
2173   status=MagickTrue;
2174   exception=(&image->exception);
2175   image_view=AcquireCacheView(image);
2176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2177   #pragma omp parallel for schedule(dynamic,4) shared(status)
2178 #endif
2179   for (y=0; y < (ssize_t) image->rows; y++)
2180   {
2181     register IndexPacket
2182       *restrict indexes;
2183
2184     register PixelPacket
2185       *restrict q;
2186
2187     register ssize_t
2188       x;
2189
2190     if (status == MagickFalse)
2191       continue;
2192     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2193     if (q == (PixelPacket *) NULL)
2194       {
2195         status=MagickFalse;
2196         continue;
2197       }
2198     indexes=GetCacheViewAuthenticIndexQueue(image_view);
2199     for (x=0; x < (ssize_t) image->columns; x++)
2200     {
2201       SetPixelPacket(image,background,q,indexes+x);
2202       q++;
2203     }
2204     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2205       status=MagickFalse;
2206   }
2207   image_view=DestroyCacheView(image_view);
2208   if (status == MagickFalse)
2209     image=DestroyImage(image);
2210   return(image);
2211 }
2212 \f
2213 /*
2214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2215 %                                                                             %
2216 %                                                                             %
2217 %                                                                             %
2218 %   R e f e r e n c e I m a g e                                               %
2219 %                                                                             %
2220 %                                                                             %
2221 %                                                                             %
2222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2223 %
2224 %  ReferenceImage() increments the reference count associated with an image
2225 %  returning a pointer to the image.
2226 %
2227 %  The format of the ReferenceImage method is:
2228 %
2229 %      Image *ReferenceImage(Image *image)
2230 %
2231 %  A description of each parameter follows:
2232 %
2233 %    o image: the image.
2234 %
2235 */
2236 MagickExport Image *ReferenceImage(Image *image)
2237 {
2238   assert(image != (Image *) NULL);
2239   if (image->debug != MagickFalse)
2240     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2241   assert(image->signature == MagickSignature);
2242   LockSemaphoreInfo(image->semaphore);
2243   image->reference_count++;
2244   UnlockSemaphoreInfo(image->semaphore);
2245   return(image);
2246 }
2247 \f
2248 /*
2249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2250 %                                                                             %
2251 %                                                                             %
2252 %                                                                             %
2253 %   R e s e t I m a g e P a g e                                               %
2254 %                                                                             %
2255 %                                                                             %
2256 %                                                                             %
2257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2258 %
2259 %  ResetImagePage() resets the image page canvas and position.
2260 %
2261 %  The format of the ResetImagePage method is:
2262 %
2263 %      MagickBooleanType ResetImagePage(Image *image,const char *page)
2264 %
2265 %  A description of each parameter follows:
2266 %
2267 %    o image: the image.
2268 %
2269 %    o page: the relative page specification.
2270 %
2271 */
2272 MagickExport MagickBooleanType ResetImagePage(Image *image,const char *page)
2273 {
2274   MagickStatusType
2275     flags;
2276
2277   RectangleInfo
2278     geometry;
2279
2280   assert(image != (Image *) NULL);
2281   assert(image->signature == MagickSignature);
2282   if (image->debug != MagickFalse)
2283     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2284   flags=ParseAbsoluteGeometry(page,&geometry);
2285   if ((flags & WidthValue) != 0)
2286     {
2287       if ((flags & HeightValue) == 0)
2288         geometry.height=geometry.width;
2289       image->page.width=geometry.width;
2290       image->page.height=geometry.height;
2291     }
2292   if ((flags & AspectValue) != 0)
2293     {
2294       if ((flags & XValue) != 0)
2295         image->page.x+=geometry.x;
2296       if ((flags & YValue) != 0)
2297         image->page.y+=geometry.y;
2298     }
2299   else
2300     {
2301       if ((flags & XValue) != 0)
2302         {
2303           image->page.x=geometry.x;
2304           if ((image->page.width == 0) && (geometry.x > 0))
2305             image->page.width=image->columns+geometry.x;
2306         }
2307       if ((flags & YValue) != 0)
2308         {
2309           image->page.y=geometry.y;
2310           if ((image->page.height == 0) && (geometry.y > 0))
2311             image->page.height=image->rows+geometry.y;
2312         }
2313     }
2314   return(MagickTrue);
2315 }
2316 \f
2317 /*
2318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2319 %                                                                             %
2320 %                                                                             %
2321 %                                                                             %
2322 %     S e p a r a t e I m a g e C h a n n e l                                 %
2323 %                                                                             %
2324 %                                                                             %
2325 %                                                                             %
2326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327 %
2328 %  SeparateImageChannel() separates a channel from the image and returns it as
2329 %  a grayscale image.  A channel is a particular color component of each pixel
2330 %  in the image.
2331 %
2332 %  The format of the SeparateImageChannel method is:
2333 %
2334 %      MagickBooleanType SeparateImageChannel(Image *image,
2335 %        const ChannelType channel)
2336 %
2337 %  A description of each parameter follows:
2338 %
2339 %    o image: the image.
2340 %
2341 %    o channel: Identify which channel to extract: RedChannel, GreenChannel,
2342 %      BlueChannel, OpacityChannel, CyanChannel, MagentaChannel,
2343 %      YellowChannel, or BlackChannel.
2344 %
2345 */
2346 MagickExport MagickBooleanType SeparateImageChannel(Image *image,
2347   const ChannelType channel)
2348 {
2349 #define SeparateImageTag  "Separate/Image"
2350
2351   CacheView
2352     *image_view;
2353
2354   ExceptionInfo
2355     *exception;
2356
2357   MagickBooleanType
2358     status;
2359
2360   MagickOffsetType
2361     progress;
2362
2363   ssize_t
2364     y;
2365
2366   assert(image != (Image *) NULL);
2367   assert(image->signature == MagickSignature);
2368   if (image->debug != MagickFalse)
2369     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2370   if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2371     return(MagickFalse);
2372   /*
2373     Separate image channels.
2374   */
2375   status=MagickTrue;
2376   if (channel == GrayChannels)
2377     image->matte=MagickTrue;
2378   progress=0;
2379   exception=(&image->exception);
2380   image_view=AcquireCacheView(image);
2381 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2382   #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2383 #endif
2384   for (y=0; y < (ssize_t) image->rows; y++)
2385   {
2386     register IndexPacket
2387       *restrict indexes;
2388
2389     register PixelPacket
2390       *restrict q;
2391
2392     register ssize_t
2393       x;
2394
2395     if (status == MagickFalse)
2396       continue;
2397     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2398     if (q == (PixelPacket *) NULL)
2399       {
2400         status=MagickFalse;
2401         continue;
2402       }
2403     indexes=GetCacheViewAuthenticIndexQueue(image_view);
2404     switch (channel)
2405     {
2406       case RedChannel:
2407       {
2408         for (x=0; x < (ssize_t) image->columns; x++)
2409         {
2410           SetGreenPixelComponent(q,GetRedPixelComponent(q));
2411           SetBluePixelComponent(q,GetRedPixelComponent(q));
2412           q++;
2413         }
2414         break;
2415       }
2416       case GreenChannel:
2417       {
2418         for (x=0; x < (ssize_t) image->columns; x++)
2419         {
2420           SetRedPixelComponent(q,GetGreenPixelComponent(q));
2421           SetBluePixelComponent(q,GetGreenPixelComponent(q));
2422           q++;
2423         }
2424         break;
2425       }
2426       case BlueChannel:
2427       {
2428         for (x=0; x < (ssize_t) image->columns; x++)
2429         {
2430           SetRedPixelComponent(q,GetBluePixelComponent(q));
2431           SetGreenPixelComponent(q,GetBluePixelComponent(q));
2432           q++;
2433         }
2434         break;
2435       }
2436       case OpacityChannel:
2437       {
2438         for (x=0; x < (ssize_t) image->columns; x++)
2439         {
2440           SetRedPixelComponent(q,GetOpacityPixelComponent(q));
2441           SetGreenPixelComponent(q,GetOpacityPixelComponent(q));
2442           SetBluePixelComponent(q,GetOpacityPixelComponent(q));
2443           q++;
2444         }
2445         break;
2446       }
2447       case BlackChannel:
2448       {
2449         if ((image->storage_class != PseudoClass) &&
2450             (image->colorspace != CMYKColorspace))
2451           break;
2452         for (x=0; x < (ssize_t) image->columns; x++)
2453         {
2454           SetRedPixelComponent(q,GetIndexPixelComponent(indexes+x));
2455           SetGreenPixelComponent(q,GetIndexPixelComponent(indexes+x));
2456           SetBluePixelComponent(q,GetIndexPixelComponent(indexes+x));
2457           q++;
2458         }
2459         break;
2460       }
2461       case TrueAlphaChannel:
2462       {
2463         for (x=0; x < (ssize_t) image->columns; x++)
2464         {
2465           SetRedPixelComponent(q,GetAlphaPixelComponent(q));
2466           SetGreenPixelComponent(q,GetAlphaPixelComponent(q));
2467           SetBluePixelComponent(q,GetAlphaPixelComponent(q));
2468           q++;
2469         }
2470         break;
2471       }
2472       case GrayChannels:
2473       {
2474         for (x=0; x < (ssize_t) image->columns; x++)
2475         {
2476           SetAlphaPixelComponent(q,PixelIntensityToQuantum(q));
2477           q++;
2478         }
2479         break;
2480       }
2481       default:
2482         break;
2483     }
2484     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2485       status=MagickFalse;
2486     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2487       {
2488         MagickBooleanType
2489           proceed;
2490
2491 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2492         #pragma omp critical (MagickCore_SeparateImageChannel)
2493 #endif
2494         proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
2495         if (proceed == MagickFalse)
2496           status=MagickFalse;
2497       }
2498   }
2499   image_view=DestroyCacheView(image_view);
2500   if (channel != GrayChannels)
2501     image->matte=MagickFalse;
2502   (void) SetImageColorspace(image,RGBColorspace);
2503   return(status);
2504 }
2505 \f
2506 /*
2507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2508 %                                                                             %
2509 %                                                                             %
2510 %                                                                             %
2511 %     S e p a r a t e I m a g e s                                             %
2512 %                                                                             %
2513 %                                                                             %
2514 %                                                                             %
2515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2516 %
2517 %  SeparateImages() returns a separate grayscale image for each channel
2518 %  specified.
2519 %
2520 %  The format of the SeparateImages method is:
2521 %
2522 %      MagickBooleanType SeparateImages(const Image *image,
2523 %        const ChannelType channel,ExceptionInfo *exception)
2524 %
2525 %  A description of each parameter follows:
2526 %
2527 %    o image: the image.
2528 %
2529 %    o channel: Identify which channels to extract: RedChannel, GreenChannel,
2530 %      BlueChannel, OpacityChannel, CyanChannel, MagentaChannel,
2531 %      YellowChannel, or BlackChannel.
2532 %
2533 %    o exception: return any errors or warnings in this structure.
2534 %
2535 */
2536 MagickExport Image *SeparateImages(const Image *image,const ChannelType channel,
2537   ExceptionInfo *exception)
2538 {
2539   Image
2540     *images,
2541     *separate_image;
2542
2543   assert(image != (Image *) NULL);
2544   assert(image->signature == MagickSignature);
2545   if (image->debug != MagickFalse)
2546     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2547   images=NewImageList();
2548   if ((channel & RedChannel) != 0)
2549     {
2550       separate_image=CloneImage(image,0,0,MagickTrue,exception);
2551       (void) SeparateImageChannel(separate_image,RedChannel);
2552       AppendImageToList(&images,separate_image);
2553     }
2554   if ((channel & GreenChannel) != 0)
2555     {
2556       separate_image=CloneImage(image,0,0,MagickTrue,exception);
2557       (void) SeparateImageChannel(separate_image,GreenChannel);
2558       AppendImageToList(&images,separate_image);
2559     }
2560   if ((channel & BlueChannel) != 0)
2561     {
2562       separate_image=CloneImage(image,0,0,MagickTrue,exception);
2563       (void) SeparateImageChannel(separate_image,BlueChannel);
2564       AppendImageToList(&images,separate_image);
2565     }
2566   if (((channel & BlackChannel) != 0) && (image->colorspace == CMYKColorspace))
2567     {
2568       separate_image=CloneImage(image,0,0,MagickTrue,exception);
2569       (void) SeparateImageChannel(separate_image,BlackChannel);
2570       AppendImageToList(&images,separate_image);
2571     }
2572   if ((channel & OpacityChannel) != 0)
2573     {
2574       separate_image=CloneImage(image,0,0,MagickTrue,exception);
2575       (void) SeparateImageChannel(separate_image,OpacityChannel);
2576       AppendImageToList(&images,separate_image);
2577     }
2578   return(images);
2579 }
2580 \f
2581 /*
2582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2583 %                                                                             %
2584 %                                                                             %
2585 %                                                                             %
2586 %   S e t I m a g e A l p h a C h a n n e l                                   %
2587 %                                                                             %
2588 %                                                                             %
2589 %                                                                             %
2590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2591 %
2592 %  SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
2593 %  channel.
2594 %
2595 %  The format of the SetImageAlphaChannel method is:
2596 %
2597 %      MagickBooleanType SetImageAlphaChannel(Image *image,
2598 %        const AlphaChannelType alpha_type)
2599 %
2600 %  A description of each parameter follows:
2601 %
2602 %    o image: the image.
2603 %
2604 %    o alpha_type:  The alpha channel type: ActivateAlphaChannel,
2605 %      CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel,
2606 %      OpaqueAlphaChannel, ResetAlphaChannel, SetAlphaChannel,
2607 %      ShapeAlphaChannel, and TransparentAlphaChannel.
2608 %
2609 */
2610 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
2611   const AlphaChannelType alpha_type)
2612 {
2613   MagickBooleanType
2614     status;
2615
2616   assert(image != (Image *) NULL);
2617   if (image->debug != MagickFalse)
2618     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2619   assert(image->signature == MagickSignature);
2620   status=MagickFalse;
2621   switch (alpha_type)
2622   {
2623     case ActivateAlphaChannel:
2624     {
2625       image->matte=MagickTrue;
2626       break;
2627     }
2628     case BackgroundAlphaChannel:
2629     {
2630       CacheView
2631         *image_view;
2632
2633       ExceptionInfo
2634         *exception;
2635
2636       IndexPacket
2637         index;
2638
2639       MagickBooleanType
2640         status;
2641
2642       MagickPixelPacket
2643         background;
2644
2645       PixelPacket
2646         pixel;
2647
2648       ssize_t
2649         y;
2650
2651       /*
2652         Set transparent pixels to background color.
2653       */
2654       if (image->matte == MagickFalse)
2655         break;
2656       if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2657         break;
2658       GetMagickPixelPacket(image,&background);
2659       SetMagickPixelPacket(image,&image->background_color,(const IndexPacket *)
2660         NULL,&background);
2661       if (image->colorspace == CMYKColorspace)
2662         ConvertRGBToCMYK(&background);
2663       index=0;
2664       SetPixelPacket(image,&background,&pixel,&index);
2665       status=MagickTrue;
2666       exception=(&image->exception);
2667       image_view=AcquireCacheView(image);
2668       #if defined(MAGICKCORE_OPENMP_SUPPORT)
2669         #pragma omp parallel for schedule(dynamic,4) shared(status)
2670       #endif
2671       for (y=0; y < (ssize_t) image->rows; y++)
2672       {
2673         register IndexPacket
2674           *restrict indexes;
2675
2676         register PixelPacket
2677           *restrict q;
2678
2679         register ssize_t
2680           x;
2681
2682         if (status == MagickFalse)
2683           continue;
2684         q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2685           exception);
2686         if (q == (PixelPacket *) NULL)
2687           {
2688             status=MagickFalse;
2689             continue;
2690           }
2691         for (x=0; x < (ssize_t) image->columns; x++)
2692         {
2693           if (q->opacity == TransparentOpacity)
2694             {
2695               SetRedPixelComponent(q,pixel.red);
2696               SetGreenPixelComponent(q,pixel.green);
2697               SetBluePixelComponent(q,pixel.blue);
2698             }
2699           q++;
2700         }
2701         if (image->colorspace == CMYKColorspace)
2702           {
2703             indexes=GetCacheViewAuthenticIndexQueue(image_view);
2704             for (x=0; x < (ssize_t) image->columns; x++)
2705               SetIndexPixelComponent(indexes+x,index);
2706           }
2707         if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2708           status=MagickFalse;
2709       }
2710       image_view=DestroyCacheView(image_view);
2711       return(status);
2712     }
2713     case DeactivateAlphaChannel:
2714     {
2715       image->matte=MagickFalse;
2716       break;
2717     }
2718     case ShapeAlphaChannel:
2719     case CopyAlphaChannel:
2720     {
2721       /*
2722         Special usage case for SeparateImageChannel(): copy grayscale color to
2723         the alpha channel.
2724       */
2725       status=SeparateImageChannel(image,GrayChannels);
2726       image->matte=MagickTrue; /* make sure transparency is now on! */
2727       if (alpha_type == ShapeAlphaChannel)
2728         {
2729           MagickPixelPacket
2730             background;
2731
2732           /*
2733             Reset all color channels to background color.
2734           */
2735           GetMagickPixelPacket(image,&background);
2736           SetMagickPixelPacket(image,&(image->background_color),(IndexPacket *)
2737             NULL,&background);
2738           (void) LevelColorsImage(image,&background,&background,MagickTrue);
2739         }
2740       break;
2741     }
2742     case ExtractAlphaChannel:
2743     {
2744       status=SeparateImageChannel(image,TrueAlphaChannel);
2745       image->matte=MagickFalse;
2746       break;
2747     }
2748     case ResetAlphaChannel: /* deprecated */
2749     case OpaqueAlphaChannel:
2750     {
2751       status=SetImageOpacity(image,OpaqueOpacity);
2752       image->matte=MagickTrue;
2753       break;
2754     }
2755     case TransparentAlphaChannel:
2756     {
2757       status=SetImageOpacity(image,TransparentOpacity);
2758       image->matte=MagickTrue;
2759       break;
2760     }
2761     case SetAlphaChannel:
2762     {
2763       if (image->matte == MagickFalse)
2764         {
2765           status=SetImageOpacity(image,OpaqueOpacity);
2766           image->matte=MagickTrue;
2767         }
2768       break;
2769     }
2770     case UndefinedAlphaChannel:
2771       break;
2772   }
2773   return(status);
2774 }
2775 \f
2776 /*
2777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2778 %                                                                             %
2779 %                                                                             %
2780 %                                                                             %
2781 %   S e t I m a g e B a c k g r o u n d C o l o r                             %
2782 %                                                                             %
2783 %                                                                             %
2784 %                                                                             %
2785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2786 %
2787 %  SetImageBackgroundColor() initializes the image pixels to the image
2788 %  background color.  The background color is defined by the background_color
2789 %  member of the image structure.
2790 %
2791 %  The format of the SetImage method is:
2792 %
2793 %      MagickBooleanType SetImageBackgroundColor(Image *image)
2794 %
2795 %  A description of each parameter follows:
2796 %
2797 %    o image: the image.
2798 %
2799 */
2800 MagickExport MagickBooleanType SetImageBackgroundColor(Image *image)
2801 {
2802   CacheView
2803     *image_view;
2804
2805   ExceptionInfo
2806     *exception;
2807
2808   IndexPacket
2809     index;
2810
2811   MagickBooleanType
2812     status;
2813
2814   MagickPixelPacket
2815     background;
2816
2817   PixelPacket
2818     pixel;
2819
2820   ssize_t
2821     y;
2822
2823   assert(image != (Image *) NULL);
2824   if (image->debug != MagickFalse)
2825     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2826   assert(image->signature == MagickSignature);
2827   if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2828     return(MagickFalse);
2829   if (image->background_color.opacity != OpaqueOpacity)
2830     image->matte=MagickTrue;
2831   GetMagickPixelPacket(image,&background);
2832   SetMagickPixelPacket(image,&image->background_color,(const IndexPacket *)
2833     NULL,&background);
2834   if (image->colorspace == CMYKColorspace)
2835     ConvertRGBToCMYK(&background);
2836   index=0;
2837   SetPixelPacket(image,&background,&pixel,&index);
2838   /*
2839     Set image background color.
2840   */
2841   status=MagickTrue;
2842   exception=(&image->exception);
2843   image_view=AcquireCacheView(image);
2844   for (y=0; y < (ssize_t) image->rows; y++)
2845   {
2846     register PixelPacket
2847       *restrict q;
2848
2849     register ssize_t
2850       x;
2851
2852     if (status == MagickFalse)
2853       continue;
2854     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2855     if (q == (PixelPacket *) NULL)
2856       {
2857         status=MagickFalse;
2858         continue;
2859       }
2860     for (x=0; x < (ssize_t) image->columns; x++)
2861       *q++=pixel;
2862     if (image->colorspace == CMYKColorspace)
2863       {
2864         register IndexPacket
2865           *restrict indexes;
2866
2867         indexes=GetCacheViewAuthenticIndexQueue(image_view);
2868         for (x=0; x < (ssize_t) image->columns; x++)
2869           SetIndexPixelComponent(indexes+x,index);
2870       }
2871     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2872       status=MagickFalse;
2873   }
2874   image_view=DestroyCacheView(image_view);
2875   return(status);
2876 }
2877 \f
2878 /*
2879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2880 %                                                                             %
2881 %                                                                             %
2882 %                                                                             %
2883 %   S e t I m a g e C h a n n e l s                                           %
2884 %                                                                             %
2885 %                                                                             %
2886 %                                                                             %
2887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2888 %
2889 %  SetImageChannels() sets the number of pixels channels associated with the
2890 %  image.
2891 %
2892 %  The format of the SetImageChannels method is:
2893 %
2894 %      MagickBooleanType SetImageChannels(Image *image,const size_t channels)
2895 %
2896 %  A description of each parameter follows:
2897 %
2898 %    o image: the image.
2899 %
2900 %    o channels:  The number of pixel channels.
2901 %
2902 */
2903 MagickExport MagickBooleanType SetImageChannels(Image *image,
2904   const size_t channels)
2905 {
2906   image->channels=channels;
2907   return(MagickTrue);
2908 }
2909 \f
2910 /*
2911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2912 %                                                                             %
2913 %                                                                             %
2914 %                                                                             %
2915 %   S e t I m a g e C o l o r                                                 %
2916 %                                                                             %
2917 %                                                                             %
2918 %                                                                             %
2919 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2920 %
2921 %  SetImageColor() set the entire image canvas to the specified color.
2922 %
2923 %  The format of the SetImageColor method is:
2924 %
2925 %      MagickBooleanType SetImageColor(Image *image,
2926 %        const MagickPixelPacket *color)
2927 %
2928 %  A description of each parameter follows:
2929 %
2930 %    o image: the image.
2931 %
2932 %    o background: the image color.
2933 %
2934 */
2935 MagickExport MagickBooleanType SetImageColor(Image *image,
2936   const MagickPixelPacket *color)
2937 {
2938   CacheView
2939     *image_view;
2940
2941   ExceptionInfo
2942     *exception;
2943
2944   MagickBooleanType
2945     status;
2946
2947   ssize_t
2948     y;
2949
2950   assert(image != (Image *) NULL);
2951   if (image->debug != MagickFalse)
2952     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2953   assert(image->signature == MagickSignature);
2954   assert(color != (const MagickPixelPacket *) NULL);
2955   image->colorspace=color->colorspace;
2956   image->matte=color->matte;
2957   image->fuzz=color->fuzz;
2958   image->depth=color->depth;
2959   status=MagickTrue;
2960   exception=(&image->exception);
2961   image_view=AcquireCacheView(image);
2962 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2963   #pragma omp parallel for schedule(dynamic,4) shared(status)
2964 #endif
2965   for (y=0; y < (ssize_t) image->rows; y++)
2966   {
2967     register IndexPacket
2968       *restrict indexes;
2969
2970     register PixelPacket
2971       *restrict q;
2972
2973     register ssize_t
2974       x;
2975
2976     if (status == MagickFalse)
2977       continue;
2978     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2979     if (q == (PixelPacket *) NULL)
2980       {
2981         status=MagickFalse;
2982         continue;
2983       }
2984     indexes=GetCacheViewAuthenticIndexQueue(image_view);
2985     for (x=0; x < (ssize_t) image->columns; x++)
2986     {
2987       SetPixelPacket(image,color,q,indexes+x);
2988       q++;
2989     }
2990     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2991       status=MagickFalse;
2992   }
2993   image_view=DestroyCacheView(image_view);
2994   return(status);
2995 }
2996 \f
2997 /*
2998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2999 %                                                                             %
3000 %                                                                             %
3001 %                                                                             %
3002 %   S e t I m a g e S t o r a g e C l a s s                                   %
3003 %                                                                             %
3004 %                                                                             %
3005 %                                                                             %
3006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3007 %
3008 %  SetImageStorageClass() sets the image class: DirectClass for true color
3009 %  images or PseudoClass for colormapped images.
3010 %
3011 %  The format of the SetImageStorageClass method is:
3012 %
3013 %      MagickBooleanType SetImageStorageClass(Image *image,
3014 %        const ClassType storage_class)
3015 %
3016 %  A description of each parameter follows:
3017 %
3018 %    o image: the image.
3019 %
3020 %    o storage_class:  The image class.
3021 %
3022 */
3023 MagickExport MagickBooleanType SetImageStorageClass(Image *image,
3024   const ClassType storage_class)
3025 {
3026   image->storage_class=storage_class;
3027   return(MagickTrue);
3028 }
3029 \f
3030 /*
3031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3032 %                                                                             %
3033 %                                                                             %
3034 %                                                                             %
3035 %   S e t I m a g e C l i p M a s k                                           %
3036 %                                                                             %
3037 %                                                                             %
3038 %                                                                             %
3039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3040 %
3041 %  SetImageClipMask() associates a clip path with the image.  The clip path
3042 %  must be the same dimensions as the image.  Set any pixel component of
3043 %  the clip path to TransparentOpacity to prevent that corresponding image
3044 %  pixel component from being updated when SyncAuthenticPixels() is applied.
3045 %
3046 %  The format of the SetImageClipMask method is:
3047 %
3048 %      MagickBooleanType SetImageClipMask(Image *image,const Image *clip_mask)
3049 %
3050 %  A description of each parameter follows:
3051 %
3052 %    o image: the image.
3053 %
3054 %    o clip_mask: the image clip path.
3055 %
3056 */
3057 MagickExport MagickBooleanType SetImageClipMask(Image *image,
3058   const Image *clip_mask)
3059 {
3060   assert(image != (Image *) NULL);
3061   if (image->debug != MagickFalse)
3062     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3063   assert(image->signature == MagickSignature);
3064   if (clip_mask != (const Image *) NULL)
3065     if ((clip_mask->columns != image->columns) ||
3066         (clip_mask->rows != image->rows))
3067       ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
3068   if (image->clip_mask != (Image *) NULL)
3069     image->clip_mask=DestroyImage(image->clip_mask);
3070   image->clip_mask=NewImageList();
3071   if (clip_mask == (Image *) NULL)
3072     return(MagickTrue);
3073   if (SetImageStorageClass(image,DirectClass) == MagickFalse)
3074     return(MagickFalse);
3075   image->clip_mask=CloneImage(clip_mask,0,0,MagickTrue,&image->exception);
3076   if (image->clip_mask == (Image *) NULL)
3077     return(MagickFalse);
3078   return(MagickTrue);
3079 }
3080 \f
3081 /*
3082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3083 %                                                                             %
3084 %                                                                             %
3085 %                                                                             %
3086 %   S e t I m a g e E x t e n t                                               %
3087 %                                                                             %
3088 %                                                                             %
3089 %                                                                             %
3090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3091 %
3092 %  SetImageExtent() sets the image size (i.e. columns & rows).
3093 %
3094 %  The format of the SetImageExtent method is:
3095 %
3096 %      MagickBooleanType SetImageExtent(Image *image,
3097 %        const size_t columns,const size_t rows)
3098 %
3099 %  A description of each parameter follows:
3100 %
3101 %    o image: the image.
3102 %
3103 %    o columns:  The image width in pixels.
3104 %
3105 %    o rows:  The image height in pixels.
3106 %
3107 */
3108 MagickExport MagickBooleanType SetImageExtent(Image *image,
3109   const size_t columns,const size_t rows)
3110 {
3111   if ((columns == 0) || (rows == 0))
3112     return(MagickFalse);
3113   image->columns=columns;
3114   image->rows=rows;
3115   return(MagickTrue);
3116 }
3117 \f
3118 /*
3119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3120 %                                                                             %
3121 %                                                                             %
3122 %                                                                             %
3123 +   S e t I m a g e I n f o                                                   %
3124 %                                                                             %
3125 %                                                                             %
3126 %                                                                             %
3127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3128 %
3129 %  SetImageInfo() initializes the `magick' field of the ImageInfo structure.
3130 %  It is set to a type of image format based on the prefix or suffix of the
3131 %  filename.  For example, `ps:image' returns PS indicating a Postscript image.
3132 %  JPEG is returned for this filename: `image.jpg'.  The filename prefix has
3133 %  precendence over the suffix.  Use an optional index enclosed in brackets
3134 %  after a file name to specify a desired scene of a multi-resolution image
3135 %  format like Photo CD (e.g. img0001.pcd[4]).  A True (non-zero) return value
3136 %  indicates success.
3137 %
3138 %  The format of the SetImageInfo method is:
3139 %
3140 %      MagickBooleanType SetImageInfo(ImageInfo *image_info,
3141 %        const unsigned int frames,ExceptionInfo *exception)
3142 %
3143 %  A description of each parameter follows:
3144 %
3145 %    o image_info: the image info.
3146 %
3147 %    o frames: the number of images you intend to write.
3148 %
3149 %    o exception: return any errors or warnings in this structure.
3150 %
3151 */
3152 MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info,
3153   const unsigned int frames,ExceptionInfo *exception)
3154 {
3155   char
3156     extension[MaxTextExtent],
3157     filename[MaxTextExtent],
3158     magic[MaxTextExtent],
3159     *q,
3160     subimage[MaxTextExtent];
3161
3162   const MagicInfo
3163     *magic_info;
3164
3165   const MagickInfo
3166     *magick_info;
3167
3168   ExceptionInfo
3169     *sans_exception;
3170
3171   Image
3172     *image;
3173
3174   MagickBooleanType
3175     status;
3176
3177   register const char
3178     *p;
3179
3180   ssize_t
3181     count;
3182
3183   unsigned char
3184     magick[2*MaxTextExtent];
3185
3186   /*
3187     Look for 'image.format' in filename.
3188   */
3189   assert(image_info != (ImageInfo *) NULL);
3190   assert(image_info->signature == MagickSignature);
3191   if (image_info->debug != MagickFalse)
3192     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3193       image_info->filename);
3194   *subimage='\0';
3195   if (frames == 0)
3196     {
3197       GetPathComponent(image_info->filename,SubimagePath,subimage);
3198       if (*subimage != '\0')
3199         {
3200           /*
3201             Look for scene specification (e.g. img0001.pcd[4]).
3202           */
3203           if (IsSceneGeometry(subimage,MagickFalse) == MagickFalse)
3204             {
3205               if (IsGeometry(subimage) != MagickFalse)
3206                 (void) CloneString(&image_info->extract,subimage);
3207             }
3208           else
3209             {
3210               size_t
3211                 first,
3212                 last;
3213
3214               (void) CloneString(&image_info->scenes,subimage);
3215               image_info->scene=StringToUnsignedLong(image_info->scenes);
3216               image_info->number_scenes=image_info->scene;
3217               p=image_info->scenes;
3218               for (q=(char *) image_info->scenes; *q != '\0'; p++)
3219               {
3220                 while ((isspace((int) ((unsigned char) *p)) != 0) ||
3221                        (*p == ','))
3222                   p++;
3223                 first=(size_t) strtol(p,&q,10);
3224                 last=first;
3225                 while (isspace((int) ((unsigned char) *q)) != 0)
3226                   q++;
3227                 if (*q == '-')
3228                   last=(size_t) strtol(q+1,&q,10);
3229                 if (first > last)
3230                   Swap(first,last);
3231                 if (first < image_info->scene)
3232                   image_info->scene=first;
3233                 if (last > image_info->number_scenes)
3234                   image_info->number_scenes=last;
3235                 p=q;
3236               }
3237               image_info->number_scenes-=image_info->scene-1;
3238               image_info->subimage=image_info->scene;
3239               image_info->subrange=image_info->number_scenes;
3240             }
3241         }
3242     }
3243   *extension='\0';
3244   GetPathComponent(image_info->filename,ExtensionPath,extension);
3245 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3246   if (*extension != '\0')
3247     if ((LocaleCompare(extension,"gz") == 0) ||
3248         (LocaleCompare(extension,"Z") == 0) ||
3249         (LocaleCompare(extension,"wmz") == 0))
3250       {
3251         char
3252           path[MaxTextExtent];
3253
3254         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
3255         path[strlen(path)-strlen(extension)-1]='\0';
3256         GetPathComponent(path,ExtensionPath,extension);
3257       }
3258 #endif
3259 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3260   if (*extension != '\0')
3261     if (LocaleCompare(extension,"bz2") == 0)
3262       {
3263         char
3264           path[MaxTextExtent];
3265
3266         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
3267         path[strlen(path)-strlen(extension)-1]='\0';
3268         GetPathComponent(path,ExtensionPath,extension);
3269       }
3270 #endif
3271   image_info->affirm=MagickFalse;
3272   sans_exception=AcquireExceptionInfo();
3273   if (*extension != '\0')
3274     {
3275       MagickFormatType
3276         format_type;
3277
3278       register ssize_t
3279         i;
3280
3281       static const char
3282         *format_type_formats[] =
3283         {
3284           "AUTOTRACE",
3285           "BROWSE",
3286           "DCRAW",
3287           "EDIT",
3288           "EPHEMERAL",
3289           "LAUNCH",
3290           "MPEG:DECODE",
3291           "MPEG:ENCODE",
3292           "PRINT",
3293           "PS:ALPHA",
3294           "PS:CMYK",
3295           "PS:COLOR",
3296           "PS:GRAY",
3297           "PS:MONO",
3298           "SCAN",
3299           "SHOW",
3300           "WIN",
3301           (char *) NULL
3302         };
3303
3304       /*
3305         User specified image format.
3306       */
3307       (void) CopyMagickString(magic,extension,MaxTextExtent);
3308       LocaleUpper(magic);
3309       /*
3310         Look for explicit image formats.
3311       */
3312       format_type=UndefinedFormatType;
3313       i=0;
3314       while ((format_type == UndefinedFormatType) &&
3315              (format_type_formats[i] != (char *) NULL))
3316       {
3317         if ((*magic == *format_type_formats[i]) &&
3318             (LocaleCompare(magic,format_type_formats[i]) == 0))
3319           format_type=ExplicitFormatType;
3320         i++;
3321       }
3322       magick_info=GetMagickInfo(magic,sans_exception);
3323       if ((magick_info != (const MagickInfo *) NULL) &&
3324           (magick_info->format_type != UndefinedFormatType))
3325         format_type=magick_info->format_type;
3326       if (format_type == UndefinedFormatType)
3327         (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3328       else
3329         if (format_type == ExplicitFormatType)
3330           {
3331             image_info->affirm=MagickTrue;
3332             (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3333           }
3334       if (LocaleCompare(magic,"RGB") == 0)
3335         image_info->affirm=MagickFalse;  /* maybe SGI disguised as RGB */
3336     }
3337   /*
3338     Look for explicit 'format:image' in filename.
3339   */
3340   *magic='\0';
3341   GetPathComponent(image_info->filename,MagickPath,magic);
3342   if (*magic == '\0')
3343     (void) CopyMagickString(magic,image_info->magick,MaxTextExtent);
3344   else
3345     {
3346       /*
3347         User specified image format.
3348       */
3349       LocaleUpper(magic);
3350       if (IsMagickConflict(magic) == MagickFalse)
3351         {
3352           (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3353           if (LocaleCompare(magic,"EPHEMERAL") != 0)
3354             image_info->affirm=MagickTrue;
3355           else
3356             image_info->temporary=MagickTrue;
3357         }
3358     }
3359   magick_info=GetMagickInfo(magic,sans_exception);
3360   sans_exception=DestroyExceptionInfo(sans_exception);
3361   if ((magick_info == (const MagickInfo *) NULL) ||
3362       (GetMagickEndianSupport(magick_info) == MagickFalse))
3363     image_info->endian=UndefinedEndian;
3364   GetPathComponent(image_info->filename,CanonicalPath,filename);
3365   (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
3366   if ((image_info->adjoin != MagickFalse) && (frames > 1))
3367     {
3368       /*
3369         Test for multiple image support (e.g. image%02d.png).
3370       */
3371       (void) InterpretImageFilename(image_info,(Image *) NULL,
3372         image_info->filename,(int) image_info->scene,filename);
3373       if ((LocaleCompare(filename,image_info->filename) != 0) &&
3374           (strchr(filename,'%') == (char *) NULL))
3375         image_info->adjoin=MagickFalse;
3376     }
3377   if ((image_info->adjoin != MagickFalse) && (frames > 0))
3378     {
3379       /*
3380         Some image formats do not support multiple frames per file.
3381       */
3382       magick_info=GetMagickInfo(magic,exception);
3383       if (magick_info != (const MagickInfo *) NULL)
3384         if (GetMagickAdjoin(magick_info) == MagickFalse)
3385           image_info->adjoin=MagickFalse;
3386     }
3387   if (image_info->affirm != MagickFalse)
3388     return(MagickTrue);
3389   if (frames == 0)
3390     {
3391       /*
3392         Determine the image format from the first few bytes of the file.
3393       */
3394       image=AcquireImage(image_info);
3395       (void) CopyMagickString(image->filename,image_info->filename,
3396         MaxTextExtent);
3397       status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3398       if (status == MagickFalse)
3399         {
3400           image=DestroyImage(image);
3401           return(MagickFalse);
3402         }
3403       if ((IsBlobSeekable(image) == MagickFalse) ||
3404           (IsBlobExempt(image) != MagickFalse))
3405         {
3406           /*
3407             Copy standard input or pipe to temporary file.
3408           */
3409           *filename='\0';
3410           status=ImageToFile(image,filename,exception);
3411           (void) CloseBlob(image);
3412           if (status == MagickFalse)
3413             {
3414               image=DestroyImage(image);
3415               return(MagickFalse);
3416             }
3417           SetImageInfoFile(image_info,(FILE *) NULL);
3418           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
3419           status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3420           if (status == MagickFalse)
3421             {
3422               image=DestroyImage(image);
3423               return(MagickFalse);
3424             }
3425           (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
3426           image_info->temporary=MagickTrue;
3427         }
3428       (void) ResetMagickMemory(magick,0,sizeof(magick));
3429       count=ReadBlob(image,2*MaxTextExtent,magick);
3430       (void) CloseBlob(image);
3431       image=DestroyImage(image);
3432       /*
3433         Check magic.xml configuration file.
3434       */
3435       sans_exception=AcquireExceptionInfo();
3436       magic_info=GetMagicInfo(magick,(size_t) count,sans_exception);
3437       if ((magic_info != (const MagicInfo *) NULL) &&
3438           (GetMagicName(magic_info) != (char *) NULL))
3439         {
3440           (void) CopyMagickString(image_info->magick,GetMagicName(magic_info),
3441             MaxTextExtent);
3442           magick_info=GetMagickInfo(image_info->magick,sans_exception);
3443           if ((magick_info == (const MagickInfo *) NULL) ||
3444               (GetMagickEndianSupport(magick_info) == MagickFalse))
3445             image_info->endian=UndefinedEndian;
3446           sans_exception=DestroyExceptionInfo(sans_exception);
3447           return(MagickTrue);
3448         }
3449       magick_info=GetMagickInfo(image_info->magick,sans_exception);
3450       if ((magick_info == (const MagickInfo *) NULL) ||
3451           (GetMagickEndianSupport(magick_info) == MagickFalse))
3452         image_info->endian=UndefinedEndian;
3453       sans_exception=DestroyExceptionInfo(sans_exception);
3454     }
3455   return(MagickTrue);
3456 }
3457 \f
3458 /*
3459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3460 %                                                                             %
3461 %                                                                             %
3462 %                                                                             %
3463 %   S e t I m a g e I n f o B l o b                                           %
3464 %                                                                             %
3465 %                                                                             %
3466 %                                                                             %
3467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3468 %
3469 %  SetImageInfoBlob() sets the image info blob member.
3470 %
3471 %  The format of the SetImageInfoBlob method is:
3472 %
3473 %      void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
3474 %        const size_t length)
3475 %
3476 %  A description of each parameter follows:
3477 %
3478 %    o image_info: the image info.
3479 %
3480 %    o blob: the blob.
3481 %
3482 %    o length: the blob length.
3483 %
3484 */
3485 MagickExport void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
3486   const size_t length)
3487 {
3488   assert(image_info != (ImageInfo *) NULL);
3489   assert(image_info->signature == MagickSignature);
3490   if (image_info->debug != MagickFalse)
3491     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3492       image_info->filename);
3493   image_info->blob=(void *) blob;
3494   image_info->length=length;
3495 }
3496 \f
3497 /*
3498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3499 %                                                                             %
3500 %                                                                             %
3501 %                                                                             %
3502 %   S e t I m a g e I n f o F i l e                                           %
3503 %                                                                             %
3504 %                                                                             %
3505 %                                                                             %
3506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3507 %
3508 %  SetImageInfoFile() sets the image info file member.
3509 %
3510 %  The format of the SetImageInfoFile method is:
3511 %
3512 %      void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3513 %
3514 %  A description of each parameter follows:
3515 %
3516 %    o image_info: the image info.
3517 %
3518 %    o file: the file.
3519 %
3520 */
3521 MagickExport void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3522 {
3523   assert(image_info != (ImageInfo *) NULL);
3524   assert(image_info->signature == MagickSignature);
3525   if (image_info->debug != MagickFalse)
3526     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3527       image_info->filename);
3528   image_info->file=file;
3529 }
3530 \f
3531 /*
3532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3533 %                                                                             %
3534 %                                                                             %
3535 %                                                                             %
3536 %   S e t I m a g e M a s k                                                   %
3537 %                                                                             %
3538 %                                                                             %
3539 %                                                                             %
3540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3541 %
3542 %  SetImageMask() associates a mask with the image.  The mask must be the same
3543 %  dimensions as the image.
3544 %
3545 %  The format of the SetImageMask method is:
3546 %
3547 %      MagickBooleanType SetImageMask(Image *image,const Image *mask)
3548 %
3549 %  A description of each parameter follows:
3550 %
3551 %    o image: the image.
3552 %
3553 %    o mask: the image mask.
3554 %
3555 */
3556 MagickExport MagickBooleanType SetImageMask(Image *image,
3557   const Image *mask)
3558 {
3559   assert(image != (Image *) NULL);
3560   if (image->debug != MagickFalse)
3561     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3562   assert(image->signature == MagickSignature);
3563   if (mask != (const Image *) NULL)
3564     if ((mask->columns != image->columns) || (mask->rows != image->rows))
3565       ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
3566   if (image->mask != (Image *) NULL)
3567     image->mask=DestroyImage(image->mask);
3568   image->mask=NewImageList();
3569   if (mask == (Image *) NULL)
3570     return(MagickTrue);
3571   if (SetImageStorageClass(image,DirectClass) == MagickFalse)
3572     return(MagickFalse);
3573   image->mask=CloneImage(mask,0,0,MagickTrue,&image->exception);
3574   if (image->mask == (Image *) NULL)
3575     return(MagickFalse);
3576   return(MagickTrue);
3577 }
3578 \f
3579 /*
3580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3581 %                                                                             %
3582 %                                                                             %
3583 %                                                                             %
3584 %     S e t I m a g e O p a c i t y                                           %
3585 %                                                                             %
3586 %                                                                             %
3587 %                                                                             %
3588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3589 %
3590 %  SetImageOpacity() sets the opacity levels of the image.
3591 %
3592 %  The format of the SetImageOpacity method is:
3593 %
3594 %      MagickBooleanType SetImageOpacity(Image *image,const Quantum opacity)
3595 %
3596 %  A description of each parameter follows:
3597 %
3598 %    o image: the image.
3599 %
3600 %    o opacity: the level of transparency: 0 is fully opaque and QuantumRange is
3601 %      fully transparent.
3602 %
3603 */
3604 MagickExport MagickBooleanType SetImageOpacity(Image *image,
3605   const Quantum opacity)
3606 {
3607   CacheView
3608     *image_view;
3609
3610   ExceptionInfo
3611     *exception;
3612
3613   MagickBooleanType
3614     status;
3615
3616   ssize_t
3617     y;
3618
3619   assert(image != (Image *) NULL);
3620   if (image->debug != MagickFalse)
3621     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3622   assert(image->signature == MagickSignature);
3623   image->matte=opacity != OpaqueOpacity ? MagickTrue : MagickFalse;
3624   status=MagickTrue;
3625   exception=(&image->exception);
3626   image_view=AcquireCacheView(image);
3627 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3628   #pragma omp parallel for schedule(dynamic,4) shared(status)
3629 #endif
3630   for (y=0; y < (ssize_t) image->rows; y++)
3631   {
3632     register PixelPacket
3633       *restrict q;
3634
3635     register ssize_t
3636       x;
3637
3638     if (status == MagickFalse)
3639       continue;
3640     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3641     if (q == (PixelPacket *) NULL)
3642       {
3643         status=MagickFalse;
3644         continue;
3645       }
3646     for (x=0; x < (ssize_t) image->columns; x++)
3647     {
3648       SetOpacityPixelComponent(q,opacity);
3649       q++;
3650     }
3651     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3652       status=MagickFalse;
3653   }
3654   image_view=DestroyCacheView(image_view);
3655   return(status);
3656 }
3657 \f
3658 /*
3659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3660 %                                                                             %
3661 %                                                                             %
3662 %                                                                             %
3663 %   S e t I m a g e T y p e                                                   %
3664 %                                                                             %
3665 %                                                                             %
3666 %                                                                             %
3667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3668 %
3669 %  SetImageType() sets the type of image.  Choose from these types:
3670 %
3671 %        Bilevel        Grayscale       GrayscaleMatte
3672 %        Palette        PaletteMatte    TrueColor
3673 %        TrueColorMatte ColorSeparation ColorSeparationMatte
3674 %        OptimizeType
3675 %
3676 %  The format of the SetImageType method is:
3677 %
3678 %      MagickBooleanType SetImageType(Image *image,const ImageType type)
3679 %
3680 %  A description of each parameter follows:
3681 %
3682 %    o image: the image.
3683 %
3684 %    o type: Image type.
3685 %
3686 */
3687 MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type)
3688 {
3689   const char
3690     *artifact;
3691
3692   ImageInfo
3693     *image_info;
3694
3695   MagickBooleanType
3696     status;
3697
3698   QuantizeInfo
3699     *quantize_info;
3700
3701   assert(image != (Image *) NULL);
3702   if (image->debug != MagickFalse)
3703     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3704   assert(image->signature == MagickSignature);
3705   status=MagickTrue;
3706   image_info=AcquireImageInfo();
3707   image_info->dither=image->dither;
3708   artifact=GetImageArtifact(image,"dither");
3709   if (artifact != (const char *) NULL)
3710     (void) SetImageOption(image_info,"dither",artifact);
3711   switch (type)
3712   {
3713     case BilevelType:
3714     {
3715       if (IsGrayImage(image,&image->exception) == MagickFalse)
3716         status=TransformImageColorspace(image,GRAYColorspace);
3717       if (IsMonochromeImage(image,&image->exception) == MagickFalse)
3718         {
3719           quantize_info=AcquireQuantizeInfo(image_info);
3720           quantize_info->number_colors=2;
3721           quantize_info->colorspace=GRAYColorspace;
3722           status=QuantizeImage(quantize_info,image);
3723           quantize_info=DestroyQuantizeInfo(quantize_info);
3724         }
3725       image->matte=MagickFalse;
3726       break;
3727     }
3728     case GrayscaleType:
3729     {
3730       if (IsGrayImage(image,&image->exception) == MagickFalse)
3731         status=TransformImageColorspace(image,GRAYColorspace);
3732       image->matte=MagickFalse;
3733       break;
3734     }
3735     case GrayscaleMatteType:
3736     {
3737       if (IsGrayImage(image,&image->exception) == MagickFalse)
3738         status=TransformImageColorspace(image,GRAYColorspace);
3739       if (image->matte == MagickFalse)
3740         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3741       break;
3742     }
3743     case PaletteType:
3744     {
3745       if (image->colorspace != RGBColorspace)
3746         status=TransformImageColorspace(image,RGBColorspace);
3747       if ((image->storage_class == DirectClass) || (image->colors > 256))
3748         {
3749           quantize_info=AcquireQuantizeInfo(image_info);
3750           quantize_info->number_colors=256;
3751           status=QuantizeImage(quantize_info,image);
3752           quantize_info=DestroyQuantizeInfo(quantize_info);
3753         }
3754       image->matte=MagickFalse;
3755       break;
3756     }
3757     case PaletteBilevelMatteType:
3758     {
3759       if (image->colorspace != RGBColorspace)
3760         status=TransformImageColorspace(image,RGBColorspace);
3761       if (image->matte == MagickFalse)
3762         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3763       (void) BilevelImageChannel(image,AlphaChannel,(double) QuantumRange/2.0);
3764       quantize_info=AcquireQuantizeInfo(image_info);
3765       status=QuantizeImage(quantize_info,image);
3766       quantize_info=DestroyQuantizeInfo(quantize_info);
3767       break;
3768     }
3769     case PaletteMatteType:
3770     {
3771       if (image->colorspace != RGBColorspace)
3772         status=TransformImageColorspace(image,RGBColorspace);
3773       if (image->matte == MagickFalse)
3774         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3775       quantize_info=AcquireQuantizeInfo(image_info);
3776       quantize_info->colorspace=TransparentColorspace;
3777       status=QuantizeImage(quantize_info,image);
3778       quantize_info=DestroyQuantizeInfo(quantize_info);
3779       break;
3780     }
3781     case TrueColorType:
3782     {
3783       if (image->colorspace != RGBColorspace)
3784         status=TransformImageColorspace(image,RGBColorspace);
3785       if (image->storage_class != DirectClass)
3786         status=SetImageStorageClass(image,DirectClass);
3787       image->matte=MagickFalse;
3788       break;
3789     }
3790     case TrueColorMatteType:
3791     {
3792       if (image->colorspace != RGBColorspace)
3793         status=TransformImageColorspace(image,RGBColorspace);
3794       if (image->storage_class != DirectClass)
3795         status=SetImageStorageClass(image,DirectClass);
3796       if (image->matte == MagickFalse)
3797         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3798       break;
3799     }
3800     case ColorSeparationType:
3801     {
3802       if (image->colorspace != CMYKColorspace)
3803         {
3804           if (image->colorspace != RGBColorspace)
3805             status=TransformImageColorspace(image,RGBColorspace);
3806           status=TransformImageColorspace(image,CMYKColorspace);
3807         }
3808       if (image->storage_class != DirectClass)
3809         status=SetImageStorageClass(image,DirectClass);
3810       image->matte=MagickFalse;
3811       break;
3812     }
3813     case ColorSeparationMatteType:
3814     {
3815       if (image->colorspace != CMYKColorspace)
3816         {
3817           if (image->colorspace != RGBColorspace)
3818             status=TransformImageColorspace(image,RGBColorspace);
3819           status=TransformImageColorspace(image,CMYKColorspace);
3820         }
3821       if (image->storage_class != DirectClass)
3822         status=SetImageStorageClass(image,DirectClass);
3823       if (image->matte == MagickFalse)
3824         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
3825       break;
3826     }
3827     case OptimizeType:
3828     case UndefinedType:
3829       break;
3830   }
3831   image->type=type;
3832   image_info=DestroyImageInfo(image_info);
3833   return(status);
3834 }
3835 \f
3836 /*
3837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3838 %                                                                             %
3839 %                                                                             %
3840 %                                                                             %
3841 %   S e t I m a g e V i r t u a l P i x e l M e t h o d                       %
3842 %                                                                             %
3843 %                                                                             %
3844 %                                                                             %
3845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3846 %
3847 %  SetImageVirtualPixelMethod() sets the "virtual pixels" method for the
3848 %  image and returns the previous setting.  A virtual pixel is any pixel access
3849 %  that is outside the boundaries of the image cache.
3850 %
3851 %  The format of the SetImageVirtualPixelMethod() method is:
3852 %
3853 %      VirtualPixelMethod SetImageVirtualPixelMethod(const Image *image,
3854 %        const VirtualPixelMethod virtual_pixel_method)
3855 %
3856 %  A description of each parameter follows:
3857 %
3858 %    o image: the image.
3859 %
3860 %    o virtual_pixel_method: choose the type of virtual pixel.
3861 %
3862 */
3863 MagickExport VirtualPixelMethod SetImageVirtualPixelMethod(const Image *image,
3864   const VirtualPixelMethod virtual_pixel_method)
3865 {
3866   assert(image != (const Image *) NULL);
3867   assert(image->signature == MagickSignature);
3868   if (image->debug != MagickFalse)
3869     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3870   return(SetPixelCacheVirtualMethod(image,virtual_pixel_method));
3871 }
3872 \f
3873 /*
3874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3875 %                                                                             %
3876 %                                                                             %
3877 %                                                                             %
3878 %     S m u s h I m a g e s                                                   %
3879 %                                                                             %
3880 %                                                                             %
3881 %                                                                             %
3882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3883 %
3884 %  SmushImages() takes all images from the current image pointer to the end
3885 %  of the image list and smushes them to each other top-to-bottom if the
3886 %  stack parameter is true, otherwise left-to-right.
3887 %
3888 %  The current gravity setting now effects how the image is justified in the
3889 %  final image.
3890 %
3891 %  The format of the SmushImages method is:
3892 %
3893 %      Image *SmushImages(const Image *images,const MagickBooleanType stack,
3894 %        ExceptionInfo *exception)
3895 %
3896 %  A description of each parameter follows:
3897 %
3898 %    o images: the image sequence.
3899 %
3900 %    o stack: A value other than 0 stacks the images top-to-bottom.
3901 %
3902 %    o offset: minimum distance in pixels between images.
3903 %
3904 %    o exception: return any errors or warnings in this structure.
3905 %
3906 */
3907
3908 static ssize_t SmushXGap(const Image *smush_image,const Image *images,
3909   const ssize_t offset,ExceptionInfo *exception)
3910 {
3911   CacheView
3912     *left_view,
3913     *right_view;
3914
3915   const Image
3916     *left_image,
3917     *right_image;
3918
3919   RectangleInfo
3920     left_geometry,
3921     right_geometry;
3922
3923   register const PixelPacket
3924     *p;
3925
3926   register ssize_t
3927     i,
3928     y;
3929
3930   size_t
3931     gap;
3932
3933   ssize_t
3934     x;
3935
3936   if (images->previous == (Image *) NULL)
3937     return(0);
3938   right_image=images;
3939   SetGeometry(smush_image,&right_geometry);
3940   GravityAdjustGeometry(right_image->columns,right_image->rows,
3941     right_image->gravity,&right_geometry);
3942   left_image=images->previous;
3943   SetGeometry(smush_image,&left_geometry);
3944   GravityAdjustGeometry(left_image->columns,left_image->rows,
3945     left_image->gravity,&left_geometry);
3946   gap=right_image->columns;
3947   left_view=AcquireCacheView(left_image);
3948   right_view=AcquireCacheView(right_image);
3949   for (y=0; y < (ssize_t) smush_image->rows; y++)
3950   {
3951     for (x=(ssize_t) left_image->columns-1; x > 0; x--)
3952     {
3953       p=GetCacheViewVirtualPixels(left_view,x,left_geometry.y+y,1,1,exception);
3954       if ((p == (const PixelPacket *) NULL) ||
3955           (GetOpacityPixelComponent(p) != TransparentOpacity) ||
3956           ((left_image->columns-x-1) >= gap))
3957         break;
3958     }
3959     i=(ssize_t) left_image->columns-x-1;
3960     for (x=0; x < (ssize_t) right_image->columns; x++)
3961     {
3962       p=GetCacheViewVirtualPixels(right_view,x,right_geometry.y+y,1,1,
3963         exception);
3964       if ((p == (const PixelPacket *) NULL) ||
3965           (GetOpacityPixelComponent(p) != TransparentOpacity) || ((x+i) >= (ssize_t) gap))
3966         break;
3967     }
3968     if ((x+i) < (ssize_t) gap)
3969       gap=(size_t) (x+i);
3970   }
3971   right_view=DestroyCacheView(right_view);
3972   left_view=DestroyCacheView(left_view);
3973   if (y < (ssize_t) smush_image->rows)
3974     return(offset);
3975   return((ssize_t) gap-offset);
3976 }
3977
3978 static ssize_t SmushYGap(const Image *smush_image,const Image *images,
3979   const ssize_t offset,ExceptionInfo *exception)
3980 {
3981   CacheView
3982     *bottom_view,
3983     *top_view;
3984
3985   const Image
3986     *bottom_image,
3987     *top_image;
3988
3989   RectangleInfo
3990     bottom_geometry,
3991     top_geometry;
3992
3993   register const PixelPacket
3994     *p;
3995
3996   register ssize_t
3997     i,
3998     x;
3999
4000   size_t
4001     gap;
4002
4003   ssize_t
4004     y;
4005
4006   if (images->previous == (Image *) NULL)
4007     return(0);
4008   bottom_image=images;
4009   SetGeometry(smush_image,&bottom_geometry);
4010   GravityAdjustGeometry(bottom_image->columns,bottom_image->rows,
4011     bottom_image->gravity,&bottom_geometry);
4012   top_image=images->previous;
4013   SetGeometry(smush_image,&top_geometry);
4014   GravityAdjustGeometry(top_image->columns,top_image->rows,top_image->gravity,
4015     &top_geometry);
4016   gap=bottom_image->rows;
4017   top_view=AcquireCacheView(top_image);
4018   bottom_view=AcquireCacheView(bottom_image);
4019   for (x=0; x < (ssize_t) smush_image->columns; x++)
4020   {
4021     for (y=(ssize_t) top_image->rows-1; y > 0; y--)
4022     {
4023       p=GetCacheViewVirtualPixels(top_view,top_geometry.x+x,y,1,1,exception);
4024       if ((p == (const PixelPacket *) NULL) ||
4025           (GetOpacityPixelComponent(p) != TransparentOpacity) || ((top_image->rows-y-1) >= gap))
4026         break;
4027     }
4028     i=(ssize_t) top_image->rows-y-1;
4029     for (y=0; y < (ssize_t) bottom_image->rows; y++)
4030     {
4031       p=GetCacheViewVirtualPixels(bottom_view,bottom_geometry.x+x,y,1,1,
4032         exception);
4033       if ((p == (const PixelPacket *) NULL) ||
4034           (GetOpacityPixelComponent(p) != TransparentOpacity) || ((y+i) >= (ssize_t) gap))
4035         break;
4036     }
4037     if ((y+i) < (ssize_t) gap)
4038       gap=(size_t) (y+i);
4039   }
4040   bottom_view=DestroyCacheView(bottom_view);
4041   top_view=DestroyCacheView(top_view);
4042   if (x < (ssize_t) smush_image->columns)
4043     return(offset);
4044   return((ssize_t) gap-offset);
4045 }
4046
4047 MagickExport Image *SmushImages(const Image *images,
4048   const MagickBooleanType stack,const ssize_t offset,ExceptionInfo *exception)
4049 {
4050 #define SmushImageTag  "Smush/Image"
4051
4052   CacheView
4053     *smush_view;
4054
4055   const Image
4056     *image;
4057
4058   Image
4059     *smush_image;
4060
4061   MagickBooleanType
4062     matte,
4063     proceed,
4064     status;
4065
4066   MagickOffsetType
4067     n;
4068
4069   RectangleInfo
4070     geometry;
4071
4072   register const Image
4073     *next;
4074
4075   size_t
4076     height,
4077     number_images,
4078     width;
4079
4080   ssize_t
4081     x_offset,
4082     y_offset;
4083
4084   /*
4085     Compute maximum area of smushed area.
4086   */
4087   assert(images != (Image *) NULL);
4088   assert(images->signature == MagickSignature);
4089   if (images->debug != MagickFalse)
4090     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
4091   assert(exception != (ExceptionInfo *) NULL);
4092   assert(exception->signature == MagickSignature);
4093   image=images;
4094   matte=image->matte;
4095   number_images=1;
4096   width=image->columns;
4097   height=image->rows;
4098   next=GetNextImageInList(image);
4099   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
4100   {
4101     if (next->matte != MagickFalse)
4102       matte=MagickTrue;
4103     number_images++;
4104     if (stack != MagickFalse)
4105       {
4106         if (next->columns > width)
4107           width=next->columns;
4108         height+=next->rows;
4109         if (next->previous != (Image *) NULL)
4110           height+=offset;
4111         continue;
4112       }
4113     width+=next->columns;
4114     if (next->previous != (Image *) NULL)
4115       width+=offset;
4116     if (next->rows > height)
4117       height=next->rows;
4118   }
4119   /*
4120     Smush images.
4121   */
4122   smush_image=CloneImage(image,width,height,MagickTrue,exception);
4123   if (smush_image == (Image *) NULL)
4124     return((Image *) NULL);
4125   if (SetImageStorageClass(smush_image,DirectClass) == MagickFalse)
4126     {
4127       InheritException(exception,&smush_image->exception);
4128       smush_image=DestroyImage(smush_image);
4129       return((Image *) NULL);
4130     }
4131   smush_image->matte=matte;
4132   (void) SetImageBackgroundColor(smush_image);
4133   status=MagickTrue;
4134   x_offset=0;
4135   y_offset=0;
4136   smush_view=AcquireCacheView(smush_image);
4137   for (n=0; n < (MagickOffsetType) number_images; n++)
4138   {
4139     SetGeometry(smush_image,&geometry);
4140     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
4141     if (stack != MagickFalse)
4142       {
4143         x_offset-=geometry.x;
4144         y_offset-=SmushYGap(smush_image,image,offset,exception);
4145       }
4146     else
4147       {
4148         x_offset-=SmushXGap(smush_image,image,offset,exception);
4149         y_offset-=geometry.y;
4150       }
4151     status=CompositeImage(smush_image,OverCompositeOp,image,x_offset,y_offset);
4152     proceed=SetImageProgress(image,SmushImageTag,n,number_images);
4153     if (proceed == MagickFalse)
4154       break;
4155     if (stack == MagickFalse)
4156       {
4157         x_offset+=(ssize_t) image->columns;
4158         y_offset=0;
4159       }
4160     else
4161       {
4162         x_offset=0;
4163         y_offset+=(ssize_t) image->rows;
4164       }
4165     image=GetNextImageInList(image);
4166   }
4167   if (stack == MagickFalse)
4168     smush_image->columns=(size_t) x_offset;
4169   else
4170     smush_image->rows=(size_t) y_offset;
4171   smush_view=DestroyCacheView(smush_view);
4172   if (status == MagickFalse)
4173     smush_image=DestroyImage(smush_image);
4174   return(smush_image);
4175 }
4176 \f
4177 /*
4178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4179 %                                                                             %
4180 %                                                                             %
4181 %                                                                             %
4182 %   S t r i p I m a g e                                                       %
4183 %                                                                             %
4184 %                                                                             %
4185 %                                                                             %
4186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4187 %
4188 %  StripImage() strips an image of all profiles and comments.
4189 %
4190 %  The format of the StripImage method is:
4191 %
4192 %      MagickBooleanType StripImage(Image *image)
4193 %
4194 %  A description of each parameter follows:
4195 %
4196 %    o image: the image.
4197 %
4198 */
4199 MagickExport MagickBooleanType StripImage(Image *image)
4200 {
4201   assert(image != (Image *) NULL);
4202   if (image->debug != MagickFalse)
4203     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4204   DestroyImageProfiles(image);
4205   (void) DeleteImageProperty(image,"comment");
4206   (void) DeleteImageProperty(image,"date:create");
4207   (void) DeleteImageProperty(image,"date:modify");
4208   (void) SetImageArtifact(image,"png:include-chunk","none,gama");
4209   return(MagickTrue);
4210 }
4211 \f
4212 /*
4213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4214 %                                                                             %
4215 %                                                                             %
4216 %                                                                             %
4217 +   S y n c I m a g e                                                         %
4218 %                                                                             %
4219 %                                                                             %
4220 %                                                                             %
4221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4222 %
4223 %  SyncImage() initializes the red, green, and blue intensities of each pixel
4224 %  as defined by the colormap index.
4225 %
4226 %  The format of the SyncImage method is:
4227 %
4228 %      MagickBooleanType SyncImage(Image *image)
4229 %
4230 %  A description of each parameter follows:
4231 %
4232 %    o image: the image.
4233 %
4234 */
4235
4236 static inline IndexPacket PushColormapIndex(Image *image,
4237   const size_t index,MagickBooleanType *range_exception)
4238 {
4239   if (index < image->colors)
4240     return((IndexPacket) index);
4241   *range_exception=MagickTrue;
4242   return((IndexPacket) 0);
4243 }
4244
4245 MagickExport MagickBooleanType SyncImage(Image *image)
4246 {
4247   CacheView
4248     *image_view;
4249
4250   ExceptionInfo
4251     *exception;
4252
4253   MagickBooleanType
4254     range_exception,
4255     status;
4256
4257   ssize_t
4258     y;
4259
4260   assert(image != (Image *) NULL);
4261   if (image->debug != MagickFalse)
4262     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4263   assert(image->signature == MagickSignature);
4264   if (image->storage_class == DirectClass)
4265     return(MagickFalse);
4266   range_exception=MagickFalse;
4267   status=MagickTrue;
4268   exception=(&image->exception);
4269   image_view=AcquireCacheView(image);
4270 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4271   #pragma omp parallel for schedule(dynamic,4) shared(status)
4272 #endif
4273   for (y=0; y < (ssize_t) image->rows; y++)
4274   {
4275     IndexPacket
4276       index;
4277
4278     register IndexPacket
4279       *restrict indexes;
4280
4281     register PixelPacket
4282       *restrict q;
4283
4284     register ssize_t
4285       x;
4286
4287     if (status == MagickFalse)
4288       continue;
4289     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4290     if (q == (PixelPacket *) NULL)
4291       {
4292         status=MagickFalse;
4293         continue;
4294       }
4295     indexes=GetCacheViewAuthenticIndexQueue(image_view);
4296     for (x=0; x < (ssize_t) image->columns; x++)
4297     {
4298       index=PushColormapIndex(image,(size_t) GetIndexPixelComponent(indexes+x),
4299         &range_exception);
4300       if (image->matte == MagickFalse)
4301         SetRGBPixelComponents(q,image->colormap+(ssize_t) index)
4302       else
4303         SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
4304       q++;
4305     }
4306     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4307       status=MagickFalse;
4308   }
4309   image_view=DestroyCacheView(image_view);
4310   if (range_exception != MagickFalse)
4311     (void) ThrowMagickException(&image->exception,GetMagickModule(),
4312       CorruptImageError,"InvalidColormapIndex","`%s'",image->filename);
4313   return(status);
4314 }
4315 \f
4316 /*
4317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4318 %                                                                             %
4319 %                                                                             %
4320 %                                                                             %
4321 %   S y n c I m a g e S e t t i n g s                                         %
4322 %                                                                             %
4323 %                                                                             %
4324 %                                                                             %
4325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4326 %
4327 %  SyncImageSettings() sync the image info options to the image.
4328 %
4329 %  The format of the SyncImageSettings method is:
4330 %
4331 %      MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
4332 %        Image *image)
4333 %      MagickBooleanType SyncImagesSettings(const ImageInfo *image_info,
4334 %        Image *image)
4335 %
4336 %  A description of each parameter follows:
4337 %
4338 %    o image_info: the image info.
4339 %
4340 %    o image: the image.
4341 %
4342 */
4343
4344 MagickExport MagickBooleanType SyncImagesSettings(ImageInfo *image_info,
4345   Image *images)
4346 {
4347   Image
4348     *image;
4349
4350   assert(image_info != (const ImageInfo *) NULL);
4351   assert(image_info->signature == MagickSignature);
4352   assert(images != (Image *) NULL);
4353   assert(images->signature == MagickSignature);
4354   if (images->debug != MagickFalse)
4355     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
4356   image=images;
4357   for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
4358     (void) SyncImageSettings(image_info,image);
4359   (void) DeleteImageOption(image_info,"page");
4360   return(MagickTrue);
4361 }
4362
4363 MagickExport MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
4364   Image *image)
4365 {
4366   char
4367     property[MaxTextExtent];
4368
4369   const char
4370     *option,
4371     *value;
4372
4373   GeometryInfo
4374     geometry_info;
4375
4376   MagickStatusType
4377     flags;
4378
4379   ResolutionType
4380     units;
4381
4382   /*
4383     Sync image options.
4384   */
4385   assert(image_info != (const ImageInfo *) NULL);
4386   assert(image_info->signature == MagickSignature);
4387   assert(image != (Image *) NULL);
4388   assert(image->signature == MagickSignature);
4389   if (image->debug != MagickFalse)
4390     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4391   option=GetImageOption(image_info,"background");
4392   if (option != (const char *) NULL)
4393     (void) QueryColorDatabase(option,&image->background_color,
4394       &image->exception);
4395   option=GetImageOption(image_info,"bias");
4396   if (option != (const char *) NULL)
4397     image->bias=SiPrefixToDouble(option,QuantumRange);
4398   option=GetImageOption(image_info,"black-point-compensation");
4399   if (option != (const char *) NULL)
4400     image->black_point_compensation=(MagickBooleanType) ParseCommandOption(
4401       MagickBooleanOptions,MagickFalse,option);
4402   option=GetImageOption(image_info,"blue-primary");
4403   if (option != (const char *) NULL)
4404     {
4405       flags=ParseGeometry(option,&geometry_info);
4406       image->chromaticity.blue_primary.x=geometry_info.rho;
4407       image->chromaticity.blue_primary.y=geometry_info.sigma;
4408       if ((flags & SigmaValue) == 0)
4409         image->chromaticity.blue_primary.y=image->chromaticity.blue_primary.x;
4410     }
4411   option=GetImageOption(image_info,"bordercolor");
4412   if (option != (const char *) NULL)
4413     (void) QueryColorDatabase(option,&image->border_color,&image->exception);
4414   option=GetImageOption(image_info,"colors");
4415   if (option != (const char *) NULL)
4416     image->colors=StringToUnsignedLong(option);
4417   option=GetImageOption(image_info,"compose");
4418   if (option != (const char *) NULL)
4419     image->compose=(CompositeOperator) ParseCommandOption(MagickComposeOptions,
4420       MagickFalse,option);
4421   option=GetImageOption(image_info,"compress");
4422   if (option != (const char *) NULL)
4423     image->compression=(CompressionType) ParseCommandOption(
4424       MagickCompressOptions,MagickFalse,option);
4425   option=GetImageOption(image_info,"debug");
4426   if (option != (const char *) NULL)
4427     image->debug=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
4428       MagickFalse,option);
4429   option=GetImageOption(image_info,"density");
4430   if (option != (const char *) NULL)
4431     {
4432       GeometryInfo
4433         geometry_info;
4434
4435       /*
4436         Set image density.
4437       */
4438       flags=ParseGeometry(option,&geometry_info);
4439       image->x_resolution=geometry_info.rho;
4440       image->y_resolution=geometry_info.sigma;
4441       if ((flags & SigmaValue) == 0)
4442         image->y_resolution=image->x_resolution;
4443     }
4444   option=GetImageOption(image_info,"depth");
4445   if (option != (const char *) NULL)
4446     image->depth=StringToUnsignedLong(option);
4447   option=GetImageOption(image_info,"endian");
4448   if (option != (const char *) NULL)
4449     image->endian=(EndianType) ParseCommandOption(MagickEndianOptions,
4450       MagickFalse,option);
4451   option=GetImageOption(image_info,"filter");
4452   if (option != (const char *) NULL)
4453     image->filter=(FilterTypes) ParseCommandOption(MagickFilterOptions,
4454       MagickFalse,option);
4455   option=GetImageOption(image_info,"fuzz");
4456   if (option != (const char *) NULL)
4457     image->fuzz=SiPrefixToDouble(option,QuantumRange);
4458   option=GetImageOption(image_info,"gravity");
4459   if (option != (const char *) NULL)
4460     image->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4461       MagickFalse,option);
4462   option=GetImageOption(image_info,"green-primary");
4463   if (option != (const char *) NULL)
4464     {
4465       flags=ParseGeometry(option,&geometry_info);
4466       image->chromaticity.green_primary.x=geometry_info.rho;
4467       image->chromaticity.green_primary.y=geometry_info.sigma;
4468       if ((flags & SigmaValue) == 0)
4469         image->chromaticity.green_primary.y=image->chromaticity.green_primary.x;
4470     }
4471   option=GetImageOption(image_info,"intent");
4472   if (option != (const char *) NULL)
4473     image->rendering_intent=(RenderingIntent) ParseCommandOption(
4474       MagickIntentOptions,MagickFalse,option);
4475   option=GetImageOption(image_info,"interlace");
4476   if (option != (const char *) NULL)
4477     image->interlace=(InterlaceType) ParseCommandOption(MagickInterlaceOptions,
4478       MagickFalse,option);
4479   option=GetImageOption(image_info,"interpolate");
4480   if (option != (const char *) NULL)
4481     image->interpolate=(InterpolatePixelMethod) ParseCommandOption(
4482       MagickInterpolateOptions,MagickFalse,option);
4483   option=GetImageOption(image_info,"loop");
4484   if (option != (const char *) NULL)
4485     image->iterations=StringToUnsignedLong(option);
4486   option=GetImageOption(image_info,"mattecolor");
4487   if (option != (const char *) NULL)
4488     (void) QueryColorDatabase(option,&image->matte_color,&image->exception);
4489   option=GetImageOption(image_info,"orient");
4490   if (option != (const char *) NULL)
4491     image->orientation=(OrientationType) ParseCommandOption(
4492       MagickOrientationOptions,MagickFalse,option);
4493   option=GetImageOption(image_info,"page");
4494   if (option != (const char *) NULL)
4495     {
4496       char
4497         *geometry;
4498
4499       geometry=GetPageGeometry(option);
4500       flags=ParseAbsoluteGeometry(geometry,&image->page);
4501       geometry=DestroyString(geometry);
4502     }
4503   option=GetImageOption(image_info,"quality");
4504   if (option != (const char *) NULL)
4505     image->quality=StringToUnsignedLong(option);
4506   option=GetImageOption(image_info,"red-primary");
4507   if (option != (const char *) NULL)
4508     {
4509       flags=ParseGeometry(option,&geometry_info);
4510       image->chromaticity.red_primary.x=geometry_info.rho;
4511       image->chromaticity.red_primary.y=geometry_info.sigma;
4512       if ((flags & SigmaValue) == 0)
4513         image->chromaticity.red_primary.y=image->chromaticity.red_primary.x;
4514     }
4515   if (image_info->quality != UndefinedCompressionQuality)
4516     image->quality=image_info->quality;
4517   option=GetImageOption(image_info,"scene");
4518   if (option != (const char *) NULL)
4519     image->scene=StringToUnsignedLong(option);
4520   option=GetImageOption(image_info,"taint");
4521   if (option != (const char *) NULL)
4522     image->taint=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
4523       MagickFalse,option);
4524   option=GetImageOption(image_info,"tile-offset");
4525   if (option != (const char *) NULL)
4526     {
4527       char
4528         *geometry;
4529
4530       geometry=GetPageGeometry(option);
4531       flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
4532       geometry=DestroyString(geometry);
4533     }
4534   option=GetImageOption(image_info,"transparent-color");
4535   if (option != (const char *) NULL)
4536     (void) QueryColorDatabase(option,&image->transparent_color,
4537       &image->exception);
4538   option=GetImageOption(image_info,"type");
4539   if (option != (const char *) NULL)
4540     image->type=(ImageType) ParseCommandOption(MagickTypeOptions,MagickFalse,
4541       option);
4542   option=GetImageOption(image_info,"units");
4543   if (option != (const char *) NULL)
4544     units=(ResolutionType) ParseCommandOption(MagickResolutionOptions,
4545       MagickFalse,option);
4546   else
4547     units = image_info->units;
4548   if (units != UndefinedResolution)
4549     {
4550       if (image->units != units)
4551         switch (image->units)
4552         {
4553           case PixelsPerInchResolution:
4554           {
4555             if (units == PixelsPerCentimeterResolution)
4556               {
4557                 image->x_resolution/=2.54;
4558                 image->y_resolution/=2.54;
4559               }
4560             break;
4561           }
4562           case PixelsPerCentimeterResolution:
4563           {
4564             if (units == PixelsPerInchResolution)
4565               {
4566                 image->x_resolution=(double) ((size_t) (100.0*2.54*
4567                   image->x_resolution+0.5))/100.0;
4568                 image->y_resolution=(double) ((size_t) (100.0*2.54*
4569                   image->y_resolution+0.5))/100.0;
4570               }
4571             break;
4572           }
4573           default:
4574             break;
4575         }
4576       image->units=units;
4577     }
4578   option=GetImageOption(image_info,"white-point");
4579   if (option != (const char *) NULL)
4580     {
4581       flags=ParseGeometry(option,&geometry_info);
4582       image->chromaticity.white_point.x=geometry_info.rho;
4583       image->chromaticity.white_point.y=geometry_info.sigma;
4584       if ((flags & SigmaValue) == 0)
4585         image->chromaticity.white_point.y=image->chromaticity.white_point.x;
4586     }
4587   ResetImageOptionIterator(image_info);
4588   for (option=GetNextImageOption(image_info); option != (const char *) NULL; )
4589   {
4590     value=GetImageOption(image_info,option);
4591     if (value != (const char *) NULL)
4592       {
4593         (void) FormatLocaleString(property,MaxTextExtent,"%s",option);
4594         (void) SetImageArtifact(image,property,value);
4595       }
4596     option=GetNextImageOption(image_info);
4597   }
4598   return(MagickTrue);
4599 }