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