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