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