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