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